日期:2014-05-20  浏览次数:20915 次

java socket通信运行出错,请高手帮下忙,多谢。。。
import java.net.*;
import java.io.*;
public class TestServer { //服务器端
public static void main(String[] args) {
try{

ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:" + br.readLine());
String str2 = br1.readLine();
while(!str2.equalsIgnoreCase("bye")){
bw.write(str2);
bw.flush();
//System.out.println("Server:" + str2);
System.out.println("Client:" + br.readLine());
str2 = br1.readLine();
}
br.close();
br1.close();
bw.close();
s.close();
ss.close();
}catch(IOException e){e.printStackTrace();}
}

}








import java.net.*;
import java.io.*;
public class TestClient{ //客服端
public static void main(String[] args) {
try{
Socket s = new Socket("127.0.0.1",6666);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
 
String str = br1.readLine();  
while(!str.equalsIgnoreCase("bye")){
bw.write(str);
bw.flush();
//System.out.println("Client:" + str);
System.out.println("Server:" + br.readLine());
str = br1.readLine();
}

br.close();
br1.close();
bw.close();
s.close();
}catch(IOException e){e.printStackTrace();}

}
}

------解决方案--------------------
这样改改就好了,bw.write()写入字符串不带换行符'\n',br1.readLine()需要读入一行字符必须以换行符'\n'结尾,如果没有换行符'\n',br1.readLine()迟迟不能获取一行数据,一直等待。


import java.net.*;
import java.io.*;
public class TestServer { //服务器端
public static void main(String[] args) {
try{

ServerSocket ss = new ServerSocket(9999); 
Socket s = ss.accept(); 
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Client:" + br.readLine());
String str2 = br1.readLine();
while(!str2.equalsIgnoreCase("bye")){
bw.write(str2+"\n");
bw.flush();
//System.out.println("Server:" + str2); 
System.out.println("Client:" + br.readLine());
str2 = br1.readLine();
}
br.close();
br1.close();
bw.close();
s.close();
ss.close();
}catch(IOException e){
e.printStackTrace();}
}
}


import java.net.*;
import java.io.*;
public class TestClient{ //客服端
public static void main(String[] args) {
try{
Socket s = new Socket("127.0.0.1",9999);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));

String str = br1.readLine();
while(!str.equalsIgnoreCase("bye")){
bw.write(str+"\n");
bw.flush();
//System.out.println("Client:" + str);