我想开发一个简单的UDP连接,并且对这种类型的编程是新的。我试图开发的是建立客户端和服务器之间的连接。首先我从客户端发送数据。它将直接到达服务器,并且工作正常。然后,服务器将相同的数据转换为大写字母后返回给客户端,同时它也会到达客户端。但是现在我每次按下按钮都会收到“SocketException”。我会在下面提供我的代码。“java.net.SocketException:套接字关闭”,而绑定一个UDP套接字
这是服务器代码:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ServerUDP extends JFrame implements ActionListener
{
Label l1;
Button b1;
DatagramSocket ds=null;
String st;
public ServerUDP()
{
setLayout(null);
setSize(300,300);
setLocation(50,50);
setTitle("Server Side");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b1=new Button("SEND TO CLIENT");
b1.setBounds(30,70,200,30);
add(b1);
b1.addActionListener(this);
l1=new Label();
l1.setBounds(30,110,150,30);
add(l1);
while(true)
{
conn();
}
}
public void connect()
{
try
{
ds=new DatagramSocket();
InetAddress ip=InetAddress.getByName("localhost");
byte[] b1=new byte[st.length()];
b1=st.getBytes();
DatagramPacket dp=new DatagramPacket(b1,st.length(),ip,9999);
ds.send(dp);
ds.close();
}
catch(Exception e)
{
System.out.println("Error 1 = "+e);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
st=l1.getText();
connect();
l1.setText("");
if(st.equals("bye"))
{
dispose();
}
}
}
public void conn()
{
try
{
ds=new DatagramSocket(6666);
byte[] b1=new byte[1024];
DatagramPacket dp=new DatagramPacket(b1,b1.length);
ds.receive(dp);
st=new String(dp.getData(),0,dp.getLength());
l1.setText(st.toUpperCase());
ds.close();
}
catch(Exception e)
{
System.out.println("Error 2 = "+e);
}
}
public static void main(String[] ar)
{
ServerUDP c=new ServerUDP();
}
}
这是客户端代码:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ClientUDP extends JFrame implements ActionListener
{
TextField t1;
Label l1,l2;
Button b1;
DatagramSocket ds=null;
String st;
public ClientUDP()
{
setLayout(null);
setSize(300,300);
setLocation(400,50);
setTitle("Client Side");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
l2=new Label("Enter Message : ");
l2.setBounds(10,20,100,30);
add(l2);
t1=new TextField();
t1.setBounds(120,20,150,30);
add(t1);
b1=new Button("SEND TO SERVER");
b1.setBounds(70,70,200,30);
add(b1);
b1.addActionListener(this);
l1=new Label();
l1.setBounds(30,110,150,30);
add(l1);
while(true)
{
connect();
}
}
public void connect()
{
try
{
ds=new DatagramSocket(9999);
byte[] b1=new byte[1024];
DatagramPacket dp=new DatagramPacket(b1,b1.length);
ds.receive(dp);
st=new String(dp.getData(),0,dp.getLength());
l1.setText(st);
ds.close();
}
catch(Exception e)
{
System.out.println("Error 3 = "+e);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
st=t1.getText();
con(st);
t1.setText("");
if(st.equals("bye"))
{
dispose();
}
}
}
public void con(String s)
{
try
{
ds=new DatagramSocket();
InetAddress ip=InetAddress.getByName("localhost");
byte[] b1=new byte[s.length()];
b1=s.getBytes();
DatagramPacket dp=new DatagramPacket(b1,s.length(),ip,6666);
ds.send(dp);
ds.close();
}
catch(Exception e)
{
System.out.println("Error 4 ="+e);
}
}
public static void main(String[] ar)
{
ClientUDP c=new ClientUDP();
}
}
我怎样才能解决这个例外?
您不需要继续创建和关闭'DatagramSockets.'这是问题的一半。您只需要一个服务器和一个客户端,并且它们可以在每种情况下在程序的整个生命周期中存在。您也不需要将客户端套接字绑定到任何特定的端口。服务器应该回复它收到的任何数据报中包含的源地址。 – EJP