#Socket Programming

16 messages · Page 1 of 1 (latest)

night flume
#

I want to establish a connection between a server and client. so, once the sever is running, and the client is on. the client need to type in the server host name and only once its authenticated, the connection is made

meager troutBOT
#

This post has been reserved for your question.

Hey @night flume! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

night flume
#

Idky when i type in the server name i get on the console for the client the connection is not being made

#

here is the client class

#
import java.net.*;
import java.io.*;

public class MyClient {
    public static String ClientHostName;

    public static void main(String[] args) {
        
        try {
             InetAddress localhost = InetAddress.getLocalHost();
               ClientHostName = localhost.getHostName();
               
        System.out.println("TCP Client starting on " + ClientHostName);



        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); //System.in is an input stream object that reads one byte of data at a time
        System.out.println("Type the name of the TCP server: ");
        String conn = userInput.readLine(); 
        
        if(conn.equals(MyServer.ServerName)) {

        Socket soc = new Socket ("localhost", 6666);
        System.out.println("Write any message:");
        String msg = userInput.readLine();

        
        PrintWriter out = new PrintWriter(soc.getOutputStream(),true);
        out.println(msg);                                        
        BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
        System.out.println(in.readLine()); 
        }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}
#

the server class:

import java.io.*;
import java.net.*;

public class MyServer {
    public static String ServerName;
    
                
    public static void main (String [] args){


    try {
    ServerSocket ss = new ServerSocket(6666); 

    Socket soc = ss.accept(); 
    
        InetAddress localhost = InetAddress.getLocalHost();
        ServerName = localhost.getHostName();

    System.out.println("TCP Server starting at host:" + ServerName + " waiting to be contacted by a client...");  

    BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream())); 
    System.out.println("A connection is established with a client :) ");
    
    
    String msg = in.readLine();
    PrintWriter out = new PrintWriter(soc.getOutputStream(),true); //sends the msg back to the client
    out.println(msg + " message is received from the client [" + MyClient.ClientHostName + "]...");
    
    }
    catch (Exception e) {
        e.printStackTrace();
}
    }
}


peak crystal
#

Hi, why do you reference "server name" variable from client while it has never been initialized?

night flume
#

its in the server class thou

#
ServerName = localhost.getHostName();
#

you mean this line?

balmy bough
#

You only want one client?

#

It seems like both sides are waiting for input once you start the server and then the client

#

Nvm

night flume
#

they should be connected via a shared network