java socket问题,写了个简单的程序,客户端输出到服务器端,服务器端接收到后返回数据,出现问题!!

server端:
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
BufferedReader br=new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));

System.out.println(br.readLine());
bw.write(br.readLine()+"received");
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}

client端:
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
BufferedReader br=new BufferedReader(new InputStreamReader(is));

bw.write("hello");
System.out.println(br.readLine());
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端的数据传不到服务器端,服务器端的数据不能返回到客户端

我修改了,直接用socket的输入输出流实现:
服务器端:
import java.io.*;
import java.net.*;
public class server{

public static void main(String args[]) throws IOException
{
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
System.out.println("New connection accepted " +socket.getInetAddress() + ":" +socket.getPort());
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
String recvStr = DIS.readLine();
System.out.println(recvStr);
PS.println(recvStr+"received");
DIS.close();
PS.close();
Is.close();
Os.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.*;
import java.net.*;
public class client{

public static void main(String args[]) throws IOException
{
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
if(socket == null)
System.out.println("error");
InputStream Is = socket.getInputStream();
OutputStream Os = socket.getOutputStream();
DataInputStream DIS = new DataInputStream(Is);
PrintStream PS = new PrintStream(Os);
PS.println("hello");
System.out.println(DIS.readLine());
DIS.close();
PS.close();
Is.close();
Os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

运行结果:
java server
server is starting
New connection accepted /127.0.0.1:45786
hello

java client
helloreceived追问

这样写我这儿也没问题,问题是这个类得readline是过期的啊,我要读一行数据还是不太方便~~~

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-08-26
嗯,问题处在了楼主没有调用bw.flush()方法,数据还都在内存里呢还没写到流中,当然看不到了,应该在server和client的bw.close()上面各加bw.flush()就Ok了追问

flush都加过了,还是不行,如果单独在client端写,在server端读,完全没有问题,但是在两端同时写和读就有问题

相似回答