主要想实现ServerDemo端可以循环接收ClientDemo端发送的一行文字
现在问题是Server端接收不到 请会的帮我看下谢谢!
Server代码
***************************************************************
import java.net.*;
import java.io.*;
class ServerDemo
{
public static void main(String [] args)
{
try
{
ServerSocket s = new ServerSocket(8309);
System.out.println("已经成功创建ServerSocket对象");
Socket socket1 = s.accept();
System.out.println("有客户端访问已经被监听到! 详细信息:" + socket1);
BufferedReader in = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
// System.out.println(in.readLine());
// PrintWriter out = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
while(true)
{
String str = in.readLine();
if(str.equals("end"))break;
System.out.println(str);
System.out.println("已收到!");
}
socket1.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Client代码
**************************************************************
import java.net.*;
import java.io.*;
class ClientDemo
{
public static void main(String[] args)
{
try
{
Socket socket2 = new Socket("127.0.0.1",8309);
System.out.println("已经成功连接到服务器!");
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket2.getOutputStream()));
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String str = in.readLine();
if(str.equals("end"))break;
out.println(str);
System.out.println("已发出!");
}
socket2.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}