我在UDP服务器和客户端之间发送数据时遇到麻烦,我试图从服务器向客户端发送字符串数组。我正在使用的循环将挂起并停止从服务器读取数据。如果我为循环选择一个较小的数字,则一切正常,但如果我尝试传输整个字符串,它将停止一半,并且客户端冻结。这是服务器,它读取文件,然后使用循环将数据发送到客户端。
```
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class UDPServer1
{
public static void main(String[] args) throws Exception
{
int portNumber=9999;
UDPConnection com1 = null;
InetAddress ipAddress = InetAddress.getLocalHost();
Message MS1 = new Message();
char x = 0;
int m = 12, c=0;
double q;
int percent=20;
String[] message = new String[255];
String m1;
x = (char)m;
m1 = x + ""; //turn next page character into string
double rand = Math.random();
//String ACK="";
System.out.println("Enter the percentage of packet losses will happen in the transmission: ");
//percent = MS1.getInt();
DatagramSocket ds=new DatagramSocket(portNumber);
File infile = new File("COSC635_P2_DataSent.txt");
Scanner sc = new Scanner(infile);
// we just need to use next page as delimiter
sc.useDelimiter(m1);
while(sc.hasNext() ) //get data packets
{
message[c] = sc.next()+m1;
c++;
}
System.out.println("UDP server Started ");
byte[] data = new byte[6500];
DatagramPacket receivedFrame = new DatagramPacket(data, data.length);
ds.receive(receivedFrame);
String ACK = new String(receivedFrame.getData(),0, receivedFrame.getLength());
//str = message[80];
//receiveData(ds);
//com1.setPort(receivedFrame.getPort());
portNumber = receivedFrame.getPort();
byte[] frame;
//dp1;
//System.out.println("Dp port is " + dp.getPort());
int i = 0;
while( i<=c-1) //error control goes here
{
//frame = (message[i].trim() ).getBytes();
//ipAddress = InetAddress.getLocalHost();
//DatagramPacket sendFrame= new DatagramPacket(frame, frame.length, ipAddress, receivedFrame.getPort());
sendData(ds, message[i].trim()+ACK+ " port number is " + portNumber, InetAddress.getLocalHost(), portNumber);
//ds.receive(receivedFrame);
//ACK = receiveData(ds);
//simulate lost packets
//rand = Math.random();
//q = rand*100;
//if(percent < (int)q)
//ds.send(sendFrame);
i++;
}
//frame = ("End of File").getBytes();
//ipAddress = InetAddress.getLocalHost();
//sendFrame= new DatagramPacket(frame, frame.length, ipAddress, receivedFrame.getPort());
//ds.send(sendFrame);
ds.close();
}
static String receiveData(DatagramSocket socket) throws IOException
{
byte buffer[];
buffer = new byte[6500];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String received = new String(packet.getData(), 1, packet.getLength() - 1).trim();
System.out.println(received);
return received;
}
static void sendData(DatagramSocket ds, String data, InetAddress ipAddress, int portNumber) throws IOException
{
byte[] frame = null;
frame = (data.trim() ).getBytes();
DatagramPacket dp1= new DatagramPacket(frame, frame.length, ipAddress, portNumber);
//simulate lost packets
//rand = Math.random();
//q = rand*100;
//if(percent < (int)q)
ds.send(dp1);
}
}
```
这是客户端的代码。
```
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.lang.Math;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class UDPClient
{
public static void main(String[] args) throws Exception
{
DatagramSocket dsocket = new DatagramSocket();
String fileContent ="";
InetAddress ipAddress = InetAddress.getLocalHost();
int socketNumber=9999;
byte[] ACK = null;
FileWriter fileWriter = new FileWriter("COSC635_P2_DataRecieved.txt");
System.out.println("UDP Client Started");
String mess = "";
Message m1;
//send frames
//Frame is the string
ACK = (" ").getBytes();
DatagramPacket dp= new DatagramPacket(ACK, ACK.length, ipAddress, socketNumber);
dsocket.send(dp);
byte[] data = new byte[6500];
String str = "";
DatagramPacket dp1 = new DatagramPacket(data, data.length);
dsocket.receive(dp1);
str = new String(dp1.getData());
int i =0;
while( i<70) //error control needs to go here
{
//ds.receive(dp1);
//str = new String(dp1.getData(), 1, dp1.getLength() - 1).trim();
mess = receiveData(dsocket);
//mess = mess + " i is " + i;
System.out.println( mess.trim());
System.out.println( "inside loop");
//fileContent = fileContent + mess;
i++;
sendData(dsocket, "Packet Received " + i + " ", ipAddress, i);
}
//
//System.out.println(fileContent);
System.out.println("outside loop");
fileWriter.write(fileContent);
fileWriter.close();
dsocket.close();
System.out.println("data socket closed");
}
static String receiveData(DatagramSocket socket) throws IOException
{
byte buffer[];
buffer = new byte[6500];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String received = new String(packet.getData(), 1, packet.getLength() - 1).trim();
System.out.println(received);
return received;
}
static void sendData(DatagramSocket ds, String data, InetAddress ipAddress, int portNumber) throws IOException
{
byte[] frame = null;
frame = (data.trim() ).getBytes();
DatagramPacket dp1= new DatagramPacket(frame, frame.length, ipAddress, portNumber);
ds.send(dp1);
}
}
```