我正在尝试在服务器和客户端之间创建双向通信,其中双方(服务器和客户端)可以相互发送消息,而无需等待另一方响应。为此,我创建了一个单独的线程来监听套接字并在屏幕上打印传入的消息。这是真正的问题:我第一次编译和运行服务器和客户端时,我运行程序后一切正常,我从服务器和客户端收到此错误消息。
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();
}
}
答案 0 :(得分:1)
这里没有服务器。服务器将创建 public override void CommitTransaction()
{
this.CollectEventsData();
base.CommitTransaction();
this.RaiseEvents(false);
}
,然后开始调用ServerSocket
。
你在这里只有两个客户。
您可能会在accept()
和readUTF()
之间感到困惑。 readLime()
只能读取readUTF()
撰写的数据,而writeUTF()
则无法读取数据。