当我将多个客户端同时连接到 Java 服务器时,套接字写入错误
Posted
技术标签:
【中文标题】当我将多个客户端同时连接到 Java 服务器时,套接字写入错误【英文标题】:Socket write error when i connect multiple client to java server simultaneously 【发布时间】:2012-09-11 07:00:39 【问题描述】:我正在创建一个桌面应用程序,其中多个客户端必须使用它们之间的套接字连接来连接到服务器。我成功连接了它们,但是当我将多个客户端同时连接到服务器时出现问题,服务器出现错误“socket write error” 我的代码在下面请给我一个答案..
public class SocketConnection implements Runnable
// password of oracle database
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
Socket clientSocket = null;
DBConnection dbConnection;
public SocketConnection()
// TODO Auto-generated constructor stub
dbConnection = new DBConnection();
if (con != null)
serverSocket = dbConnection.createSocket();
if (serverSocket != null)
System.out.println("Server Started. Looking for the connections.");
System.out.println("Listening Port:8888.......");
Thread t = new Thread(this);
t.start();
@Override
public void run()
// TODO Auto-generated method stub
while (true)
try
clientSocket = serverSocket.accept();
System.out.println("Connection Accepted");
Connect m_connect = new Connect(clientSocket);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
public class Connect implements Runnable
Socket clientSocket = null;
Thread t = null;
private ResultSet res1;
private ResultSet res2;
Statement stmt;
private File mkFolder;
public Connect(Socket clientSocke)
// TODO Auto-generated constructor stub
this.clientSocket = clientSocke;
try
stmt = con.createStatement();
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
try
catch (Exception e)
// TODO: handle exception
e.printStackTrace();
t = new Thread(this);
t.start();
@Override
public void run()
try
dataInputStream = new DataInputStream(
clientSocket.getInputStream());
dataOutputStream = new DataOutputStream(
clientSocket.getOutputStream());
System.out.println("Connection established::"
+ clientSocket.getInetAddress());
String pass = dataInputStream.readUTF();
System.out.println(pass);
if (pass.equals("1"))
//here is read n write operation
else if (pass.equals("3"))
//here is read n write operation
else if (pass.equals("2"))
//here is read n write operation
else if (pass.equals("4"))
//here is read n write operation
else if (pass.equals("5"))
//here is read n write operation
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
finally
if (socket != null)
try
socket.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
if (dataOutputStream != null)
try
dataOutputStream.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
if (dataInputStream != null)
try
dataInputStream.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
if (stmt != null)
try
stmt.close();
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
【问题讨论】:
你知道是哪一行报错了吗? 读写操作出错.. 哪个读/写操作,在代码中抛出了那个错误?很明显这是一个读/写操作,因为它在错误消息的标题中。 这是我的堆栈跟踪... Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.DataOutputStream.write(Unknown Source) at java.io.DataOutputStream.writeUTF(Unknown Source) at java.io.DataOutputStream.writeUTF(Unknown Source) ) 在 ConnectOracle$SocketConnection$Connect.run(ConnectOracle.java:529) n 第 529 行是.. dataOutputStream.writeUTF(res2 .getString(t));通过=1; 【参考方案1】:您为什么要以这种低级方式执行此操作?选择一个有据可查的 API 并通过它进行通信!
您是否已经阅读过这样的文章:Multithread client server chat on sockets. Load test. recv failed
【讨论】:
我不知道..如何实现这个 很抱歉,我不能教你开发协议。为什么你不使用 RMI 或其他?然后你可以发送愚蠢的简单 pojos 来代表你的命令并返回一些数据。但我永远不会自己实现这个,你尝试什么 感谢您的考虑。!!但是我的问题现在已经解决了。。我也会尝试学习这个。。以上是关于当我将多个客户端同时连接到 Java 服务器时,套接字写入错误的主要内容,如果未能解决你的问题,请参考以下文章