客户端 - 服务器UDP连接

问题描述 投票:0回答:1

我试图一次将数据从服务器发送到客户端1位。无论何时从服务器收到一点,客户端都应该回复ACK。

当前发生的是当客户端将初始请求发送到服务器时,数据将传递到服务器。当服务器将所需数据发送到客户端时,数据将循环回自身,并且客户端将处于无限期等待挂起状态。

我已经为客户端和服务器附加了下面的代码。请看一下,并告诉我哪里出错了。

客户端:

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;
}
}
java udp client-server
1个回答
0
投票

如果您的客户端没有收到服务器的响应,可能是因为服务器没有发送响应。

serverSocket.send(sendPacket1);只调用if(i!= strlen)

您是否尝试在if块中的控制台中输出一些文本以查看是否/何时进入该循环?

© www.soinside.com 2019 - 2024. All rights reserved.