我写了个简单的socket通信,发现在读写上老是出错,大概意思就是这样:
1.我让服务端写个不停,服务端就抛出Connection reset by peer: socket write error错,客户端正常
2.我让服务端少写,这里又分2种情况:
2.1:服务端这样写:while(i<3)即只写3条数据,此时客户端服务端都不报错
2.2:服务端直接用WriteUTF()写,此时只有客户端报错
服务端:
import java.util.*;
import java.net.*;
import java.io.*;public class ssss { public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8800);
Socket s = server.accept();
DataInputStream m_in=new DataInputStream(s.getInputStream());
DataOutputStream m_out=new DataOutputStream(s.getOutputStream());
int i=0;
//while(true) 1.服务端写个不停
//while(i<3) 2.1.服务端用while少写数据
//2.1.服务端少写,但是不用while,直接用下面的wrietUTF()
{
m_out.writeUTF("Server msg");
i++;
}
}}
客户端:
import java.net.*;
import java.io.*;
public class cccc {
DataOutputStream m_out;
DataInputStream m_in;
Socket client;
public cccc() throws IOException{
int i=1;
client=new Socket("127.0.0.1",8800);
m_in=new DataInputStream(client.getInputStream());
m_out=new DataOutputStream(client.getOutputStream());
String msg=null;
while(i<20){
try {
msg=m_in.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
if(msg!=null)
System.out.println(msg);
}
}
public static void main(String arg[]) throws UnknownHostException, IOException{
new cccc();
}
}
1.我让服务端写个不停,服务端就抛出Connection reset by peer: socket write error错,客户端正常
2.我让服务端少写,这里又分2种情况:
2.1:服务端这样写:while(i<3)即只写3条数据,此时客户端服务端都不报错
2.2:服务端直接用WriteUTF()写,此时只有客户端报错
服务端:
import java.util.*;
import java.net.*;
import java.io.*;public class ssss { public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8800);
Socket s = server.accept();
DataInputStream m_in=new DataInputStream(s.getInputStream());
DataOutputStream m_out=new DataOutputStream(s.getOutputStream());
int i=0;
//while(true) 1.服务端写个不停
//while(i<3) 2.1.服务端用while少写数据
//2.1.服务端少写,但是不用while,直接用下面的wrietUTF()
{
m_out.writeUTF("Server msg");
i++;
}
}}
客户端:
import java.net.*;
import java.io.*;
public class cccc {
DataOutputStream m_out;
DataInputStream m_in;
Socket client;
public cccc() throws IOException{
int i=1;
client=new Socket("127.0.0.1",8800);
m_in=new DataInputStream(client.getInputStream());
m_out=new DataOutputStream(client.getOutputStream());
String msg=null;
while(i<20){
try {
msg=m_in.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
if(msg!=null)
System.out.println(msg);
}
}
public static void main(String arg[]) throws UnknownHostException, IOException{
new cccc();
}
}
解决方案 »
- 请问各位高手 jface tableviewer 中如何获得 当前cell 或者 当前行数列数
- package url does not exist(急就加红的地方编译不过去,谢谢!)
- 为什么必须通过改变框架大小才能进行重绘
- 关于永中office
- 类ShowEscapeSequences是公共的,应在名为ShowEscapeSequences.java的文件中声明 ???
- 这种系统使用EJB是否会加快速度呢
- 问题三:如何调用类?
- 如何写下面语句,!!?来者有分!
- 为什么将checkbox放到checkboxgroup中就变成了radio了,如果我想用真正的checkboxgroup呢?
- java 中中文编码问题 是不是 与本地系统有关?
- 100分讨论这种情况该用一般类,单例类还是静态方法类???
- GUI小程序中的奇怪现象 求高手解答 在Eclipse中可以执行 但在dos下不能编译!
import java.net.*;
import java.io.*;public class ssss { public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8800); Socket s = server.accept();
DataOutputStream m_out = new DataOutputStream(s.getOutputStream()); int i = 0;
while (i++<1000){
m_out.writeUTF("Server msg"+i);
}
}}
import java.net.*;
import java.io.*;public class cccc {
DataOutputStream m_out;
DataInputStream m_in;
Socket client; public cccc() throws IOException {
int i = 1;
client = new Socket("127.0.0.1", 8800);
m_in = new DataInputStream(client.getInputStream());
m_out = new DataOutputStream(client.getOutputStream());
String msg = null; while (m_in.available() > 0) { try {
msg = m_in.readUTF(); } catch (IOException e) {
e.printStackTrace();
}
if (msg != null)
System.out.println(msg); }
} public static void main(String arg[]) throws UnknownHostException,
IOException {
new cccc();
}}
当我加上一个if(m_in.available() > 0)的时候,客户端读不到内容了,不知道怎么回事
没有啊?? 我运行了就可以了啊 int i = 0;
while (true) {
m_out.writeUTF("Server msg");
i++;
}
就是这里吧? 我把注释去掉了就是这样了。我能一直接受数据。