套接字流的Client/Server程序的字符串传输问题
按教材上说的,我写了一个简单的套接字流的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){}
}
}
我的源程序是这样的:
一. 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){}
}
}
...全文
相关推荐
SQL Server Native Client 10.0 驱动
常用的 SQL Server 驱动程序,不需要多做说明了吧
C++Socket Server端代码,解析Client端发来的字符串!
实现Client与Server的通信,传输字符串并解析字符串!
UDP Server Client 范例程序.doc
UDP Server Client 范例程序.doc UDP Server Client 范例程序.doc UDP Server Client 范例程序.doc
Microsoft SQL Server 2012 Native Client
Microsoft SQL Server 2012 Native Client.msi PHP连接SQL SERVER 时报【未发现数据源名称并且未指定默认驱动程序】
基于Java的Socket多客户端Client-Server聊天程序的实现
主要介绍了基于Java的Socket多客户端Client-Server聊天程序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


Java SE

