Pawan Nirpal
Pawan Nirpal

Reputation: 622

UDP Client-Server in Java both on local machine, The Client Program is not responding

I am simulating a basic client-server chat application in Java using UDP protocol, the problem is my client program is not responding to the message sent by the server program, To begin with, the server sends an initiation message to which the client is supposed to read a string from its respective PowerShell terminal and that message is to be sent over to the server program, As I debugged the code I realized that server program was able to send the initiation message but client program was not taking input from its terminal, Can you see with this, forgive me if this sounds a bit silly but I am a beginner in computer network's course. (Both Client and Server run on my local machine)

Server Program

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.*;



public class ServerUDP {
    private static int SERVERPORT;
    private static int CLIENTPORT;
    private static InetAddress SERVERADDRESS;
    public DatagramSocket ServerSocket;
    public BufferedReader br;
    private int MSGLEN;
    
    void ConnectionSetup() throws Exception{
        SERVERPORT = 3001;
        CLIENTPORT = 3000;
        MSGLEN = 100;
        SERVERADDRESS = InetAddress.getLocalHost();
        br = new BufferedReader(new InputStreamReader(System.in));
        ServerSocket = new DatagramSocket(SERVERPORT);
    }

    void ConnectionClose() throws Exception{
        br.close();
        ServerSocket.close();
    }

    void SendMessage(String mString) throws Exception{
        byte[] BuffToClient = new byte[this.MSGLEN];
        BuffToClient = mString.getBytes();
        DatagramPacket PktToClient = new DatagramPacket(BuffToClient, BuffToClient.length,SERVERADDRESS,CLIENTPORT);
        ServerSocket.send(PktToClient);
    }

    String GetMessage() throws Exception{
        byte[] BuffFromClient = new byte[this.MSGLEN];
        DatagramPacket PktFromClient = new DatagramPacket(BuffFromClient,BuffFromClient.length);
        ServerSocket.receive(PktFromClient);
        BuffFromClient = PktFromClient.getData();
        return new String(BuffFromClient,StandardCharsets.UTF_8);
    }

    void Converse() throws Exception{
        this.SendMessage("Hello Client, How are you?");
        System.out.println("msg sent");
        while(true){
            String CliMsg = this.GetMessage();
            if(CliMsg.equals("bye")){
                this.SendMessage("Ok bye");
                break;
            }else{
                System.out.println("Client Says : "+CliMsg);
                this.SendMessage(br.readLine());
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ServerUDP Server = new ServerUDP();
        Server.ConnectionSetup();
        Server.Converse();
        Server.ConnectionClose();
    }    
}

Client Program

import java.io.*;
import java.net.InetAddress;
import java.nio.charset.*;
import java.net.*;


public class ClientUDP {
    public static int CLIENTPORT;
    public static int SERVERPORT;
    public InetAddress CLIENTADD;
    public InetAddress SERVERADDRESS;
    public int MSGLEN;
    public static BufferedReader br;
    public DatagramSocket clientSocket;

    void ConnectionSetup() throws Exception{
        CLIENTPORT = 3000;
        SERVERPORT = 3001;
        MSGLEN = 100;
        CLIENTADD = InetAddress.getLocalHost();
        SERVERADDRESS = InetAddress.getLocalHost();
        br = new BufferedReader(new InputStreamReader(System.in));
        clientSocket = new DatagramSocket(CLIENTPORT);
    }

    void ConnectionClose() throws Exception{
        br.close();
        clientSocket.close();
    }

    void SendMessage(String mString) throws Exception{
        byte[] BuffToServer = new byte[this.MSGLEN];
        BuffToServer = mString.getBytes();
        DatagramPacket PktToServer = new DatagramPacket(BuffToServer,BuffToServer.length,CLIENTADD,SERVERPORT);
        clientSocket.send(PktToServer);
    }

    String GetMessage() throws Exception{
        byte[] BuffFromServer = new byte[this.MSGLEN];
        DatagramPacket PktFromServer = new DatagramPacket(BuffFromServer, BuffFromServer.length);
        clientSocket.receive(PktFromServer);
        BuffFromServer = PktFromServer.getData();
        System.out.println("msg recived");
        for(byte b : BuffFromServer){
            System.out.println(b);
        }
        return new String(BuffFromServer,StandardCharsets.UTF_8);
    }

    void Converse() throws Exception{
        System.out.println(this.GetMessage());
        System.out.println("got the msg");
        while(true){
            String MsgToSrvr = br.readLine();
            this.SendMessage(MsgToSrvr);
            System.out.println("Server Says : "+this.GetMessage());
            if(MsgToSrvr.equals("bye")){
                break;
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ClientUDP Client = new ClientUDP();
        Client.ConnectionSetup();
        Client.Converse();
        Client.ConnectionClose();
    }
}

Upvotes: 0

Views: 377

Answers (1)

mangusta
mangusta

Reputation: 3544

I guess your problem is that you turned on the server first, and then the client.

If you turn on the client first, it's going to block on line clientSocket.receive(PktFromServer); waiting for the server to send something. After that, you turn on the server, it sends the message right after starting and client receives the message, prints it out and waits for the user to print something in the console. In the meantime, server waits for client to send something. Then client waits for server and server waits for user, etc.

However if you turn on the server first, it spits out the message to nowhere, because UDP does not need to establish connection prior to sending something. After that, you turn on the client which starts waiting for the server, but it will not receive anything because the initial message from server was lost. So they will stuck waiting for each other to send something.

This is the reason of your situation. You didn't mention how you want the client and the server to mutually behave so I don't suggest anything now. You may specify that and I will suggest a modification.

Upvotes: 1

Related Questions