gpt4 book ai didi

java - 连接建立后服务器套接字程序不执行

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:33 25 4
gpt4 key购买 nike

与客户端成功连接后,我在服务器端的 java 程序未执行。客户端在这里发起连接,并根据程序要求向服务器发送一些数据。但是服务器从 inputStreamBuffer 读取后不执行代码行。这是代码:

对于服务器::

//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 地址是否属于同一网络(子网)或不同。请检查一下。预先感谢:)

最佳答案

像往常一样。您正在读取行,但没有发送行。您需要在消息中添加行终止符。

关于java - 连接建立后服务器套接字程序不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com