我試圖創建服務器和客戶端之間的雙向通信,其中雙方(服務器和客戶端)可以互相發送消息,而無需等待另一方回覆。爲此,我創建了一個單獨的線程來偵聽套接字並在屏幕上打印傳入的消息。這是真正的問題:當我運行程序後,我第一次編譯和運行服務器和客戶端時,一切正常,我從服務器和客戶端都收到此錯誤消息。Java套接字編程,雙向通信
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at Build.work(c.java:18)
at s.main(s.java:83)
Exception in thread "Thread-0" Exception in thread "main" java.lang.NullPointerException
at Build.run(c.java:59)
at java.lang.Thread.run(Thread.java:748)
java.lang.NullPointerException
at Build.speak(c.java:35)
at c.main(c.java:85)
服務器
import java.net.*;
import java.io.*;
class Build implements Runnable
{
int port = 3500;
String input1;
ServerSocket server;
Socket socket;
DataInputStream in;
DataOutputStream out;
Console reader;
public void work()
{
try
{
server = new ServerSocket(port);
socket = server.accept();
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
reader = System.console();
}
catch(IOException e){
e.printStackTrace();}
}
public void speak()
{
try
{
while(true)
{
input1 = reader.readLine();
out.writeUTF(input1);
if(input1.equals("stop"))
{
in.close();
out.close();
socket.close();
server.close();
System.exit(0);
}
}
}
catch (IOException e){
e.printStackTrace();}
}
public void run()
{
String input2;
try
{
while(true)
{
input2 = in.readUTF();
System.out.println(input2);
if(input2.equals("stop"))
{
in.close();
out.close();
socket.close();
server.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();}
}
}
public class server
{
public static void main(String[]args)
{
Build object = new Build();
object.work();
Thread t1 = new Thread(object);
t1.start();
object.speak();
}
}
客戶
import java.net.*;
import java.io.*;
class Build implements Runnable
{
String ip = "127.0.0.1";
int port = 3500;
String input1;
Socket socket;
DataInputStream in;
DataOutputStream out;
Console reader;
public void work()
{
try
{
socket = new Socket(ip, port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
reader = System.console();
}
catch(IOException e){
e.printStackTrace();}
}
public void speak()
{
try
{
while(true)
{
input1 = reader.readLine();
out.writeUTF(input1);
if(input1.equals("stop"))
{
in.close();
out.close();
socket.close();
System.exit(0);
}
}
}
catch (IOException e){
e.printStackTrace();}
}
public void run()
{
String input2;
try
{
while(true)
{
input2 = in.readUTF();
System.out.println(input2);
if(input2.equals("stop"))
{
in.close();
out.close();
socket.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();}
}
}
public class client
{
public static void main(String[]args)
{
Build object = new Build();
object.work();
Thread t1 = new Thread(object);
t1.start();
object.speak();
}
}
@arghavan沒有'afterward'這樣的詞。 – EJP
您的編輯沒有「可以隨意增加」,它從根本上改變了問題。不要這樣做。 – EJP