双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

套接字流的Client/Server程序的字符串传输问题

firedrose firedrose 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
按教材上说的,我写了一个简单的套接字流的Client/Server程序功能是Client把键盘的输入信息发送到Server上。然后Server显示出来。语法正确,编译也完全通过。问题出来了,在输入完一个信息之后,按一下回车,正确传送到Server上,可是再想在Client上输入下一个字符信息时却不能输入,真是不解,请大家帮帮忙,帮我解决这一问题!谢谢
我的源程序是这样的:
一. Client端
import java.io.*;
import java.net.*;
public class talkclient
{
public static void main(String args[])
{
String s="";
try{
Socket socket=new Socket("firedrose200959",1000);
PrintStream os=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream is=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataInputStream in=new DataInputStream(new BufferedInputStream(System.in));
while(s!="Bye")
{
s=in.readLine();
os.println(s);
os.flush();
System.out.println("Client:"+s);
System.out.println("Server:"+is.readLine());
}
os.close();
is.close();
socket.close();
}catch(Exception e){}
}
}
二.Server端
import java.io.*;
import java.net.*;
public class talkserver
{
public static void main(String args[])
{
try{

ServerSocket server=new ServerSocket(1000);


Socket socket=server.accept();

String s="";
DataInputStream is=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
PrintStream os=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream sin=new DataInputStream(new BufferedInputStream(System.in));
System.out.println("Client:"+is.readLine());
s=sin.readLine();
while(!s.equals("Bye"))
{
os.print(s);
os.flush();
System.out.println("Server:"+s);
System.out.println("Client:"+is.readLine());
s=sin.readLine();
}
is.close();
os.close();
socket.close();
server.close();
}catch(Exception e){}
}
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
firedrose
Java SE 62,621 成员 307,251 内容