2012-03-09 3 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

예. 나는이 다섯 가지 명령으로이 작업을 수행했다. System.out.println ("n 값 :"+ n); send = send.concat (n + ":"); o = output.substring (i, i + 1); System.out.println ("o의 값은 :"+ o); PS - 회신에서 회선을 어떻게 구분합니까? 나는 Shift + Enter를 사용하고 있지만 선들은 모두 따로 따로 모여있다. – Shayan