我想開發一個簡單的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