Server socket program not executing after connection establishment

纵然是瞬间 提交于 2020-01-30 08:58:24

问题


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:)


回答1:


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



来源:https://stackoverflow.com/questions/46138456/server-socket-program-not-executing-after-connection-establishment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!