Socket Client = new Socket("ip", port);
private ObjectOutputStream out;
out = new ObjectOutputStream(Client.getOutputStream());//line3
out.flush();
我有一个套接字程序。当我有一个客户端时,没有问题。但是当客户端在第二个及之后的客户端中超过一个时,程序在第 3 行等待
。它没有错误和异常。问题是什么?
TCP = new Thread(new Runnable() {
@Override
public void run() {
if ("TCP".equals(Protocol)) {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
RunServer();
}
}
});
TCPWait = new Thread(new Runnable() {
@Override
public void run() {
try {
WaitForConnection();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
TCPWait.start();
TCP.start();
public void RunServer() {
try {
while (true) {
{
accessLock.lock();
if(CanContinue == false)
condition.await();
try {
// WaitForConnection();
GetStreams();
ProcessConnection();
accessLock.unlock();
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "ارتباط قطع شد" + e.getMessage());
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e.getMessage());
}
public void WaitForConnection() throws IOException {
jTextArea1.append("لطفا تا برقرای ارتباط صبر کنید.../n ");
accessLock.lock();
connection = server.accept();
CanContinue = true;
condition.signal();
accessLock.unlock();
jTextArea1.append("ارتباط از " + connection.getInetAddress().getHostName() + "\n");
}
public void GetStreams() {
try {
Socket s = new Socket();
s = connection;
out = new ObjectOutputStream(s.getOutputStream());
out.flush();
in = new ObjectInputStream(s.getInputStream());
jTextArea1.append("در حال برقرای ارتباط...\n");
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "مشکل در برقرای ارتباط!\n");
}
}
public void ProcessConnection() {
SendData("ارتباط برقرا شد\n");
try {
jTextArea1.append((String) in.readObject() + "\n");
} catch (Exception e) {
}
String message = "";
do {
try {
jTextArea1.append((String) in.readObject() + "\n");
} catch (Exception e) {
}
} while (message != "Exit");
}
WaitForConnection()
是线程中的监听器,runserver()
发送和接收另一个线程中的消息。
最佳答案
Socket s = new Socket();
s = connection;
那里有严重错误。
关于java - ObjectOutputStream 需要服务器响应吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9421416/