简体   繁体   中英

Server is not sending the response in java using socket programming

When I'm sending some data to server from client, server is not giving me the response. When I enter all the data till Gender, the cursor blinks there. It seems it wants the user to insert more data. It is accepting data as much as I'm inserting. I have provided all my code below

My client side code is

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

My server side code:

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){
        }
    }  
}

Tell me why my server is not sending a response. Correct it so that it can send respond to the client.

Age, car price and gender inputs, not read with send order. It must be read from the client in order.

Change below snippet. Also, add sc.nextLine() to skip newline after int read.

Server

age = sc.nextInt();
carPrice = sc.nextInt();
sc.nextLine();  // skip newline
gender = sc.next();

... instead of

age = sc.nextInt();
gender = sc.next();
carPrice = sc.nextInt();

On the client-side, read order is not matched with send order. Also sc.next() reads one word, but you send many words while sending gender. So you have to change method with sc.nextLine()

Client

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

.. instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM