gpt4 book ai didi

java - java中客户端和服务器端的通信方式

转载 作者:行者123 更新时间:2023-11-30 06:35:15 24 4
gpt4 key购买 nike

我有一个聊天程序。现在代码可以通过命令行在客户端和服务器之间进行通信。但它在运行时给出了一个异常(java.net.SocketException:套接字已关闭)。请帮我解决这个问题。

在一个java聊天程序中,客户端和服务器之间的通信是如何实现的?

即。

客户端<-->服务器(在服务器和客户端之间)

             or

客户端A<-->服务器<-->客户端B(服务器充当两个客户端之间的桥梁)

是否可以通过单个套接字实现 2 路通信?还有其他方法吗?如何同时与多个客户端通信?

服务器代码

class Server
{
ServerSocket server;
Socket client;
public Server()
{
try
{
server = new ServerSocket(2000);
System.out.println("\tServer Started..........");
while (true)
{
client = server.accept();
Send objsend = new Send(client);
Recive objrecive = new Recive(client);
//client.close();
}
}
catch (Exception e)
{
System.out.println("Exception4 " + e);
}
}
public static void main(String arg[])
{
new Server();
}
}
class Recive implements Runnable
{
Socket client;
public Recive(Socket client1)
{
client=client1;
Thread trsend=new Thread(this);
trsend.start();
}
public void run()
{
ObjectInputStream ois;
Message M=new Message();
try
{
ois = new ObjectInputStream(client.getInputStream());
M = (Message)ois.readObject();
M.display();
ois.close();
}
catch (Exception e)
{
System.out.println("Exception1 " + e);
}
}
}
class Send implements Runnable
{
Socket client;
public Send(Socket client1)
{
client=client1;
Thread trrecive=new Thread(this);
trrecive.start();
}
public void run()
{
Message M=new Message();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try
{
System.out.println("Me(server)");
M.strmessage=br.readLine();
ObjectOutputStream oos=new ObjectOutputStream(cli ent.getOutputStream());
oos.writeObject((Message)M);
oos.flush();
oos.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
}

客户端代码

class Client
{
public static void main(String arg[])
{
try
{

Send objsend=new Send();
Recive objrecive=new Recive();
}
catch(Exception e)
{
System.out.println("Exception "+e);
}

}
}
class Send implements Runnable
{

public Send()
{
Thread trsend=new Thread(this);
trsend.start();
}
public void run()
{

try
{
Message M=new Message();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true)
{
System.out.println("Me(client)");
M.strmessage=br.readLine();

Socket client=new Socket("localhost",2000);
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
oos.writeObject((Message)M);

oos.flush();
oos.close();
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
class Recive implements Runnable
{
public Recive()
{
Thread trrecive=new Thread(this);
trrecive.start();
}
public void run()
{

try
{
while(true)
{
Socket client=new Socket("localhost",2000);
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
Message CNE=(Message)ois.readObject();
CNE.display();

ois.close();
}

}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}

最佳答案

首先,不要在每次运行时关闭流。

其次,检查你使用的服务器端口是否空闲。

关于java - java中客户端和服务器端的通信方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com