Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器未使用套接字编程以java发送响应_Java_Sockets_Client Server - Fatal编程技术网

服务器未使用套接字编程以java发送响应

服务器未使用套接字编程以java发送响应,java,sockets,client-server,Java,Sockets,Client Server,当我从客户机向服务器发送一些数据时,服务器并没有给我响应。当我输入所有数据时,光标在那里闪烁。它似乎希望用户插入更多数据。它接受的数据和我插入的数据一样多。我在下面提供了我的所有代码 我的客户端代码是 import java.io.IOException; 导入java.io.PrintStream; 导入java.net.Socket; 导入java.util.Scanner; 公共类客户端{ 公共静态void main(字符串[]args)引发IOException{ 温度,温度,温度1;

当我从客户机向服务器发送一些数据时,服务器并没有给我响应。当我输入所有数据时,光标在那里闪烁。它似乎希望用户插入更多数据。它接受的数据和我插入的数据一样多。我在下面提供了我的所有代码

我的客户端代码是

import java.io.IOException;
导入java.io.PrintStream;
导入java.net.Socket;
导入java.util.Scanner;
公共类客户端{
公共静态void main(字符串[]args)引发IOException{
温度,温度,温度1;
字符串性别,s_temp;
扫描仪sc=新的扫描仪(System.in);
插座=新插座(“127.0.0.1”,1342);
Scanner sc1=新的扫描仪(socket.getInputStream());
PrintStream p=新的PrintStream(socket.getOutputStream(),true);
System.out.println(“输入您的年龄:”;
年龄=sc.nextInt();
p、 println(年龄);
System.out.println(“输入车辆成本:”);
carPrice=sc.nextInt();
p、 println(carPrice);
System.out.println(“输入您的性别:”;
性别=sc.next();
p、 println(性别);
p、 冲洗();
temp=sc1.nextInt();
系统输出打印项次(温度);
s_temp=sc1.next();
System.out.println(“性别为:”+s_temp);
temp1=sc1.nextInt();
系统输出打印项次(temp1);
socket.close();
}
}
我的服务器端代码:

公共类服务器{
公共静态void main(字符串args[])引发IOException、SQLException{
ServerSocket ssocket=null;
套接字=空;
System.out.println(“服务器侦听……*”;
ssocket=新服务器插座(1342);
while(true){
试一试{
socket=ssocket.accept();
System.out.println(“已建立连接”);
ServerThread st=新的ServerThread(套接字);
st.start();
}
捕获(例外e){
System.out.println(“连接错误…”);
}
}
} 
}
类ServerThread扩展线程{
套接字s1=null;
扫描仪sc=空;
PrintStream p=null;
国际年龄,卡普里斯,温度;
字符串性别,temp1;
公共服务器线程(套接字){
s1=s;
}
公开募捐{
试一试{
sc=新扫描仪(s1.getInputStream());
p=新的打印流(s1.getOutputStream(),true);
年龄=sc.nextInt();
性别=sc.next();
carPrice=sc.nextInt();
p、 println(年龄*2岁);
p、 println(carPrice);
p、 println(“您的性别为:”+性别);
s1.关闭();
System.out.println(“连接关闭…”);
}
捕获(例外e){
}
}  
}

告诉我为什么我的服务器没有发送响应。更正它,以便它可以向客户端发送响应。

年龄、汽车价格和性别输入,不使用发送订单读取。必须按顺序从客户端读取

更改下面的代码段。另外,添加
sc.nextLine()

服务器

age=sc.nextInt();
carPrice=sc.nextInt();
sc.nextLine();//跳过换行符
性别=sc.next();
。。。而不是

age=sc.nextInt();
性别=sc.next();
carPrice=sc.nextInt();
在客户端,读取顺序与发送顺序不匹配。另外,
sc.next()
读取一个单词,但在发送性别时发送了许多单词。因此,您必须使用
sc.nextLine()

客户

temp=sc1.nextInt();
系统输出打印项次(温度);
temp1=sc1.nextInt();
系统输出打印项次(temp1);
sc1.nextLine();//跳过换行符
s_temp=sc1.nextLine();
System.out.println(“性别为:”+s_temp);
。。而不是

temp=sc1.nextInt();
系统输出打印项次(温度);
s_temp=sc1.next();
System.out.println(“性别为:”+s_temp);
temp1=sc1.nextInt();
系统输出打印项次(temp1);

现在开始工作了谢谢