服务器未使用套接字编程在 java 中发送响应
Server is not sending the response in java using socket programming
当我从客户端向服务器发送一些数据时,服务器没有给我响应。当我输入所有数据直到性别时,光标在那里闪烁。它似乎希望用户插入更多数据。它正在接受与我插入的数据一样多的数据。我在下面提供了我的所有代码
我的客户端代码是
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
int age,carPrice,temp,temp1;
String gender, s_temp;
Scanner sc = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1",1342);
Scanner sc1 = new Scanner(socket.getInputStream());
PrintStream p = new PrintStream(socket.getOutputStream(), true);
System.out.println("Enter Your Age: ");
age = sc.nextInt();
p.println(age);
System.out.println("Enter the Cost of Vehicle: ");
carPrice = sc.nextInt();
p.println(carPrice);
System.out.println("Enter Your Gender: ");
gender = sc.next();
p.println(gender);
p.flush();
temp = sc1.nextInt();
System.out.println(temp);
s_temp = sc1.next();
System.out.println("The Gender is: " + s_temp);
temp1 = sc1.nextInt();
System.out.println(temp1);
socket.close();
}
}
我的服务器端代码:
public class Server {
public static void main(String args[]) throws IOException, SQLException{
ServerSocket ssocket = null;
Socket socket = null;
System.out.println("Server Listening.......*");
ssocket=new ServerSocket(1342);
while(true){
try{
socket = ssocket.accept();
System.out.println("Connection Established.");
ServerThread st = new ServerThread(socket);
st.start();
}
catch(Exception e){
System.out.println("Connection Error....");
}
}
}
}
class ServerThread extends Thread{
Socket s1 =null;
Scanner sc = null;
PrintStream p=null;
int age,carPrice,temp;
String gender,temp1;
public ServerThread(Socket s){
s1=s;
}
public void run(){
try{
sc = new Scanner(s1.getInputStream());
p = new PrintStream(s1.getOutputStream(),true);
age = sc.nextInt();
gender = sc.next();
carPrice = sc.nextInt();
p.println(age*2);
p.println(carPrice);
p.println("Your gender is: " +gender);
s1.close();
System.out.println("Connection Closed....");
}
catch(Exception e){
}
}
}
告诉我为什么我的服务器没有发送响应。更正它以便它可以向客户端发送响应。
年龄、汽车价格和性别输入,发送订单时未阅读。必须按顺序从客户端读取。
更改以下代码段。此外,添加 sc.nextLine()
以在 int 读取后跳过换行符。
服务器
age = sc.nextInt();
carPrice = sc.nextInt();
sc.nextLine(); // skip newline
gender = sc.next();
...而不是
age = sc.nextInt();
gender = sc.next();
carPrice = sc.nextInt();
在客户端,读取顺序与发送顺序不匹配。还有sc.next()
读了一个字,但是你在发性的同时发了很多字。所以你必须用 sc.nextLine()
改变方法
客户
temp = sc1.nextInt();
System.out.println(temp);
temp1 = sc1.nextInt();
System.out.println(temp1);
sc1.nextLine(); // skip newline
s_temp = sc1.nextLine();
System.out.println("The Gender is: " + s_temp);
.. 而不是
temp = sc1.nextInt();
System.out.println(temp);
s_temp = sc1.next();
System.out.println("The Gender is: " + s_temp);
temp1 = sc1.nextInt();
System.out.println(temp1);
当我从客户端向服务器发送一些数据时,服务器没有给我响应。当我输入所有数据直到性别时,光标在那里闪烁。它似乎希望用户插入更多数据。它正在接受与我插入的数据一样多的数据。我在下面提供了我的所有代码
我的客户端代码是
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
int age,carPrice,temp,temp1;
String gender, s_temp;
Scanner sc = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1",1342);
Scanner sc1 = new Scanner(socket.getInputStream());
PrintStream p = new PrintStream(socket.getOutputStream(), true);
System.out.println("Enter Your Age: ");
age = sc.nextInt();
p.println(age);
System.out.println("Enter the Cost of Vehicle: ");
carPrice = sc.nextInt();
p.println(carPrice);
System.out.println("Enter Your Gender: ");
gender = sc.next();
p.println(gender);
p.flush();
temp = sc1.nextInt();
System.out.println(temp);
s_temp = sc1.next();
System.out.println("The Gender is: " + s_temp);
temp1 = sc1.nextInt();
System.out.println(temp1);
socket.close();
}
}
我的服务器端代码:
public class Server {
public static void main(String args[]) throws IOException, SQLException{
ServerSocket ssocket = null;
Socket socket = null;
System.out.println("Server Listening.......*");
ssocket=new ServerSocket(1342);
while(true){
try{
socket = ssocket.accept();
System.out.println("Connection Established.");
ServerThread st = new ServerThread(socket);
st.start();
}
catch(Exception e){
System.out.println("Connection Error....");
}
}
}
}
class ServerThread extends Thread{
Socket s1 =null;
Scanner sc = null;
PrintStream p=null;
int age,carPrice,temp;
String gender,temp1;
public ServerThread(Socket s){
s1=s;
}
public void run(){
try{
sc = new Scanner(s1.getInputStream());
p = new PrintStream(s1.getOutputStream(),true);
age = sc.nextInt();
gender = sc.next();
carPrice = sc.nextInt();
p.println(age*2);
p.println(carPrice);
p.println("Your gender is: " +gender);
s1.close();
System.out.println("Connection Closed....");
}
catch(Exception e){
}
}
}
告诉我为什么我的服务器没有发送响应。更正它以便它可以向客户端发送响应。
年龄、汽车价格和性别输入,发送订单时未阅读。必须按顺序从客户端读取。
更改以下代码段。此外,添加 sc.nextLine()
以在 int 读取后跳过换行符。
服务器
age = sc.nextInt();
carPrice = sc.nextInt();
sc.nextLine(); // skip newline
gender = sc.next();
...而不是
age = sc.nextInt();
gender = sc.next();
carPrice = sc.nextInt();
在客户端,读取顺序与发送顺序不匹配。还有sc.next()
读了一个字,但是你在发性的同时发了很多字。所以你必须用 sc.nextLine()
客户
temp = sc1.nextInt();
System.out.println(temp);
temp1 = sc1.nextInt();
System.out.println(temp1);
sc1.nextLine(); // skip newline
s_temp = sc1.nextLine();
System.out.println("The Gender is: " + s_temp);
.. 而不是
temp = sc1.nextInt();
System.out.println(temp);
s_temp = sc1.next();
System.out.println("The Gender is: " + s_temp);
temp1 = sc1.nextInt();
System.out.println(temp1);