建立连接后服务器套接字程序未执行 [英] Server socket program not executing after connection establishment

查看:43
本文介绍了建立连接后服务器套接字程序未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与客户端成功连接后,服务器端的Java程序未执行.客户端在这里启动连接,并根据程序要求根据服务器发送一些数据.但是Server从inputStreamBuffer读取后不会执行代码行.这是代码:

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:

对于服务器::

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

客户:

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

}

此程序实现了子网游戏,该子网游戏又从客户端询问两个IP地址和一个子网掩码,并将此数据发送到服务器,服务器以回复方式检查两个IP地址是属于同一网络(子网)还是不同的.请检查一下.在此先感谢:)

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

推荐答案

照常.您正在阅读线路,但未发送线路.您需要在消息中添加行终止符.

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

这篇关于建立连接后服务器套接字程序未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆