#TCP协议实现类似于qq聊天,我想实现一直对话的效果,但是客户端发送一个以后,服务器端一直收不到,也不关闭!
#客户端代码如下:
public class Tclicent {
public void chatc() {
try {
Socket soc = new Socket( "192.168.3.48",100);
try {
InputStream i1 = soc.getInputStream();
InputStreamReader i2 = new InputStreamReader(i1);
BufferedReader b1 = new BufferedReader(i2);
BufferedWriter b2 = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
Scanner sc = new Scanner(System.in);
while (true) {
String s = sc.nextLine();
System.out.println("客户端" + s);
b2.write(s+"\n");
String s1 = b1.readLine();
System.out.println(s1);
}
} finally {
soc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
#服务器端代码如下:
public class Tserver {
public void chats() {
try {
ServerSocket ser = new ServerSocket(100);
Socket accept = ser.accept();
try {
InputStream i1 = accept.getInputStream();
InputStreamReader i2 = new InputStreamReader(i1);
BufferedReader b1 = new BufferedReader(i2);
BufferedWriter b2 = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
Scanner sc = new Scanner(System.in);
while (true) {
String s = b1.readLine();
System.out.println(s);
String s1 = sc.nextLine();
System.out.println("客户端:" + s1);
b2.write(s1+"\n");
}
} finally {
ser.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}