下面是小弟客户端发送接收数据对的代码,总是在读取服务器返回的数据报错“connection reset”:
public class Client {
public static String DoClient(String host, int port, String data) {
Socket soc = null;
OutputStream out = null;
InputStream is = null;
BufferedReader br = null;
String info = null;
try {
soc = new Socket(host, port);
out = soc.getOutputStream();
String length = String.format("%04d", data.length());
out.write(length.getBytes());
out.write(data.getBytes());
out.flush();
soc.shutdownOutput();
System.out.println("发送的数据长度为" + length);
is = soc.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
while ((info = br.readLine()) != null) {
System.out.println("我是客户端,服务器说:" + info);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
out.close();
if (soc != null) {
soc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return info;
}
}
之前没写过这种方式的通讯模式,跪求大神给个详细的解决方案!!!