Java 客户端服务器UDP连接
我试图将数据从服务器一次发送1位到客户端。每当客户端从服务器接收到一个比特时,它都应该用ACK进行响应 当前发生的情况是,当客户机向服务器发送初始请求时,数据被传递到服务器。当服务器向客户机发送所需的数据时,数据被循环回自身,客户机处于无限等待状态 我在下面附上了客户端和服务器的代码。请看一看,并就我的错误提出建议 客户端:Java 客户端服务器UDP连接,java,udp,client-server,Java,Udp,Client Server,我试图将数据从服务器一次发送1位到客户端。每当客户端从服务器接收到一个比特时,它都应该用ACK进行响应 当前发生的情况是,当客户机向服务器发送初始请求时,数据被传递到服务器。当服务器向客户机发送所需的数据时,数据被循环回自身,客户机处于无限等待状态 我在下面附上了客户端和服务器的代码。请看一看,并就我的错误提出建议 客户端: import java.io.IOException; import java.net.*; import java.util.Scanner; c
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
class UDPClient extends Thread implements Runnable
{
DatagramSocket clientSocket;
InetAddress IPAddress;
public static void main(String args[]) throws Exception
{
try
{
UDPClient t1 = new UDPClient();
Thread t = new Thread(t1);
t.start();
}
catch (Exception e)
{
System.out.println("Error!");
}
}
public UDPClient() throws Exception
{
this.clientSocket = new DatagramSocket();
this.IPAddress = InetAddress.getByName("Localhost");
clientSocket.setSoTimeout(5000);
}
public void run()
{
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String m ="1001";
String checksum = "1111";
String checksumSend = "";
String sentence="";
String sentence1="";
String dataRequired="";
try
{
dataRequired = dataRequired.concat(m+":Data");
sendData = dataRequired.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
checksumSend = checksumSend.concat(checksum+":checksum");
sendData = checksumSend.getBytes();
DatagramPacket sendPacket2 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket2);
Scanner in = new Scanner(System.in);
System.out.println("Enter the Window size: ");
int s = in.nextInt();
String WinSize="";
WinSize = WinSize.concat(s+":Windowsize");
sendData = WinSize.getBytes();
DatagramPacket sendPacket3 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket3);
String finished="Finished";
sendData = finished.getBytes();
DatagramPacket sendPacket6 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket6);
do
{
try
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println("I am getting executed");
clientSocket.receive(receivePacket); //After this step, nothing gets executed
sentence = new String(receivePacket.getData(),0,receivePacket.getLength());
System.out.println("Received from Server: " + sentence);
if(receivePacket != null)
sentence1 = "ACK";
sendData = sentence1.getBytes();
DatagramPacket sendPacket4 =
new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket4);
}
catch (SocketTimeoutException a)
{
System.out.println("Timed out!");
}
}while(sentence!=null);
}
catch (IOException e)
{
System.err.println(e);
}
finally
{
clientSocket.close();
}
}
}
服务器端:
import java.io.IOException;
import java.net.*;
public class UDPServer extends Thread implements Runnable
{
DatagramSocket serverSocket;
public static void main(String args[]) throws Exception
{
try
{
UDPServer t1 = new UDPServer();
Thread t = new Thread(t1);
t.start();
}
catch (Exception e)
{
System.out.println("Error!");
}
}
public UDPServer() throws Exception
{
this.serverSocket = new DatagramSocket(9876);
}
public void run()
{
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
String checksum="";
String Data = "";
String WinSize = "";
String carry = "0";
String output="";
String n,o;
String output1;
String sentence1="";
int i,j=0;
while(true)
{
String sentence="";
try
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress IPAddress = receivePacket.getAddress();
sentence = new String( receivePacket.getData(),0,receivePacket.getLength());
if(sentence.contains("checksum"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
checksum = sentence.substring(0,i);
}
}
System.out.println("Checksum as specified by client is: " + checksum);
}
else if(sentence.contains("Data"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
Data = sentence.substring(0,i);
}
}
System.out.println("Data requested by client is: " + Data);
}
else if(sentence.contains("Windowsize"))
{
int len = sentence.length();
for(i=0;i<len;i++)
{
if(sentence.charAt(i)==':')
{
WinSize = sentence.substring(0,i);
}
}
System.out.println("Window Size is: " + WinSize);
}
else if(sentence.contains("Finished"))
{
output1 = checksumAdd(carry,Data,checksum);
output = reverse(output1);
System.out.println("Checksum Addition before complementing digits = "+output);
output = complement(output);
output = reverse(output);
System.out.println("Checksum Addition after complementing digits = "+output);
int WindowSize = Integer.parseInt(WinSize);
int strlen = Data.length();
do
{
for(i=j;i<(WindowSize+j);i++)
{
if(i!=strlen)
{
String send = "";
n = Data.substring(i,i+1);
System.out.println("Value of n is: "+n);
send = send.concat(n+":");
o = output.substring(i,i+1);
System.out.println("Value of o is: "+o);
send = send.concat(o);
sendData = send.getBytes();
DatagramPacket sendPacket1 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
serverSocket.send(sendPacket1);
}
else
break;
}
j+=WindowSize;
DatagramPacket receivePacket2 = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket2);
sentence1 = new String( receivePacket2.getData(),0,receivePacket2.getLength());
System.out.println("sentence 1 is: "+sentence1); //To be removed. Used for testing purposes
}while(i!=strlen);
}
}
catch (IOException e)
{
System.err.println(e);
}
}
}
public static String complement(String output) {
String temp="";
int len = output.length();
for(int i=len;i>0;i--)
{
String t = output.substring(i-1,i);
if(t.equals("0"))
temp = temp.concat("1");
else if(t.equals("1"))
temp = temp.concat("0");
}
return temp;
}
public static String reverse(String output)
{
String temp="";
int len = output.length();
for(int i=len;i>0;i--)
{
String t = output.substring(i-1,i);
temp = temp.concat(t);
}
return temp;
}
public static String checksumAdd(String carry, String Data,String checksum)
{
int strlen = Data.length();
int flag=0;
String output="";
String output2="";
String n,che;
String sum = null;
for(int i=strlen;i>0;i--)
{
n=Data.substring(i-1,i);
che = checksum.substring(i-1,i);
if(n.equals("0") && che.equals("0") && carry.equals("0"))
{
sum = "0";
carry = "0";
}
else if(n.equals("0") && che.equals("0") && carry.equals("1"))
{
sum = "1";
carry = "0";
}
else if(n.equals("0") && che.equals("1") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("0") && che.equals("1") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("0") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && che.equals("0") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("1") && carry.equals("0"))
{
sum = "0";
carry = "1";
}
else if(n.equals("1") && che.equals("1") && carry.equals("1"))
{
sum = "1";
carry = "1";
}
output = output.concat(sum);
}
if(carry.equals("1"))
{
n = output.substring(0,1);
if(n.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1"))
{
sum = "0";
carry = "1";
}
output2 = output2.concat(sum);
for(int i=strlen-1;i>0;i--)
{
n=Data.substring(i-1,i);
if(n.equals("0") && carry.equals("0"))
{
sum = "0";
carry = "0";
}
else if(n.equals("0") && carry.equals("1"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && carry.equals("0"))
{
sum = "1";
carry = "0";
}
else if(n.equals("1") && carry.equals("1"))
{
sum = "0";
carry = "1";
}
output2 = output2.concat(sum);
}
flag = 1;
}
if (flag==1)
return output2;
return output;
}
}
import java.io.IOException;
导入java.net。*;
公共类UDPServer扩展线程实现可运行
{
DatagramSocket服务器套接字;
公共静态void main(字符串args[])引发异常
{
尝试
{
UDPServer t1=新的UDPServer();
螺纹t=新螺纹(t1);
t、 start();
}
捕获(例外e)
{
System.out.println(“错误!”);
}
}
公共UDPServer()引发异常
{
this.serverSocket=newdatagramsocket(9876);
}
公开募捐
{
字节[]接收数据=新字节[1024];
字节[]发送数据=新字节[1024];
字符串校验和=”;
字符串数据=”;
字符串WinSize=“”;
字符串进位=“0”;
字符串输出=”;
字符串n,o;
字符串输出1;
字符串语句1=“”;
int i,j=0;
while(true)
{
字符串句子=”;
尝试
{
DatagramPacket receivePacket=新的DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(接收数据包);
InetAddress IPAddress=receivePacket.getAddress();
句子=新字符串(receivePacket.getData(),0,receivePacket.getLength());
if(句子包含(“校验和”))
{
int len=句子长度();
对于(i=0;i0;i--)
{
n=数据子串(i-1,i);
如果(n等于(“0”)&进位等于(“0”))
{
sum=“0”;
进位=“0”;
}
如果(n等于(“0”)&进位等于(“1”))
{
sum=“1”;
进位=“0”;
}
如果(n等于(“1”)&进位等于(“0”))
{
sum=“1”;
进位=“0”;
}
否则,如果(n等于(“1”)&进位等于(“1”))
{
sum=“0”;
进位=“1”;
}
output2=output2.concat(总和);
}
flag=1;
}
如果(标志==1)
返回输出2;
返回输出;
}
}
如果客户端没有收到服务器的响应,可能是因为服务器没有发送响应
serverSocket.send(sendPacket1);
仅当(i!=strlen)时才调用
您是否尝试在if块内的控制台中输出一些文本,以查看是否/何时进入该循环?是的。我已经使用以下5个命令完成了此操作:System.out.println(“n的值为:”+n);send=send.concat(n+“:”);o=output.substring(I,I+1);System.out.println(“o的值为:”+o);PS-如何在回复中分隔行?我使用Shift+Enter,但行都集中在一起而不是分开。不可能通过UDP发送一位,而您没有这样做。“UDP连接”在术语上已经是一个矛盾。不清楚您在问什么。