服务器端的我的java程序在与客户端成功连接后没有执行。客户端在此启动连接,并根据程序要求根据服务器发送一些数据。但是,在从inputStreamBuffer读取后,Server不会执行代码行。这是代码:
对于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);
}
}
}
客户端:
//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地址是否属于同一网络(子网)或不同。 请检查一下。 在此先感谢:)
答案 0 :(得分:1)