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

server端:ServerSocketss=null;Socketsocket=null;try{ss=newServerSocket(8888);System.out... 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();
}
}
}
客户端的数据传不到服务器端,服务器端的数据不能返回到客户端
展开
 我来答
逯北Nm
推荐于2016-08-25 · TA获得超过347个赞
知道小有建树答主
回答量:172
采纳率:0%
帮助的人:148万
展开全部
我修改了,直接用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是过期的啊,我要读一行数据还是不太方便~~~
molaifeng
2011-08-26 · TA获得超过5273个赞
知道大有可为答主
回答量:1629
采纳率:50%
帮助的人:1788万
展开全部
嗯,问题处在了楼主没有调用bw.flush()方法,数据还都在内存里呢还没写到流中,当然看不到了,应该在server和client的bw.close()上面各加bw.flush()就Ok了
追问
flush都加过了,还是不行,如果单独在client端写,在server端读,完全没有问题,但是在两端同时写和读就有问题
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式