2012-03-09 257 views
1

我试图一次从服务器发送数据到客户端1位。每当客户端收到服务器的一小部分响应时,客户端都会回应一个ACK。客户端 - 服务器UDP连接

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

我附上了下面的客户端和服务器代码。请看看它并告诉我哪里出错了。

客户端:

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; 
} 
} 

回答

0

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

serverSocket.send(sendPacket1);仅在(i!= strlen)时才会调用

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

+0

是。我已经完成了这5个命令: System.out.println(“n的值是:”+ n); send = send.concat(n +“:”); o = output.substring(i,i + 1); System.out.println(“o的值是:”+ o); PS - 你如何在回复中分隔线条?我正在使用Shift + Enter,但是这些线条全部聚集在一起而不是分开 – Shayan 2012-03-09 18:26:53