Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My java program at server side not executing after successful connection with client. Client initiate the connection here and sends some data according to server according to program requirement. But Server do not execute the code-lines after reading from inputStreamBuffer. Here is the code:

For Server ::

//Pgm implements server portion for subnet game 
import java.util.*;
import java.net.*;
import java.io.*;
public class ServerTCP{
    public static void main(String[] args)
    {
        Scanner sc1 = new Scanner(System.in);
        int port = sc1.nextInt();//Enter port number to bind with
        try{
        ServerSocket w_s = new ServerSocket(port);
            while(true){
                    System.out.println("Starting Server");
                    Socket sok = w_s.accept();//welcome Socket
                    System.out.println("Request Came");
                    BufferedReader sc = new BufferedReader(new   InputStreamReader(sok.getInputStream()));
                    DataOutputStream out = new DataOutputStream(sok.getOutputStream());
                    String str = sc.readLine();//not executing after this line
                    System.out.println("Data received"+str);
                    String[] val = str.split("E");
                    String ip1 = val[0];
                    System.out.println(ip1);
                    String ip2 = val[1];
                    System.out.println("1");
                    String sub = val[2];                    
                    String[] ip1_parts = ip1.split(".");
                    String[] ip2_parts = ip2.split(".");
                    String[] sub_parts = sub.split(".");
                    String result="true";
                    for(int i=0;i<4;i++){
                    if(((Integer.parseInt(ip1_parts[i])) & (Integer.parseInt(sub_parts[i]))) != (Integer.parseInt(ip2_parts[i]) & Integer.parseInt(sub_parts[i]))){
                        result="false";
                        break;
                    }
                        }
                 out.writeBytes(result);
                 }
            }
             catch(Exception e){
                System.out.println(e);
                }
        }
}       

Client:

//Program implements client machine for TCP connection
import java.util.Scanner;
import java.io.*;
import java.net.*;
public class ClientTCP{
public static void main(String[] args)
{
    String ip;
    int host;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter ip address of host/server:");
    ip=sc.next();
    System.out.println("Enter Server port number:");
    host=sc.nextInt();
    try{
        System.out.println("Enter both ip addresses one by one followed by subnet mask to transmit");
        String ip1=sc.next();
        String ip2=sc.next();
        String sub=sc.next();
        String send = ip1+"E"+ip2+"E"+sub;          
        Socket sok = new Socket(ip,host);
        System.out.println("Connection Established");
        DataOutputStream out = new DataOutputStream(sok.getOutputStream());

        BufferedReader rev = new BufferedReader(new InputStreamReader(sok.getInputStream()));

        System.out.println("Sending String:"+send);
        out.writeBytes(send);
        System.out.println("Data Sent");
        String res = rev.readLine();
        if(res.equals("true")){
            System.out.println("Server says: Ip addresses belongs to same network");
            }else{
            System.out.println("Ip addresses belongs to different network");
            }
        out.close();
        rev.close();
        sok.close();
        }
    catch(Exception e){
        System.out.println(e);
        }
    }

}

This program implements subnet game which in turns asks two ip addresses and one subnet mask from client and sends this data to server which in reply checks whether both ip addresses belongs to same network(subnet) or different. Please check this. Thanks in advance:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

As usual. You are reading lines but you aren't sending lines. You need to add a line terminator to the message.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...