0

This code is about client and server communication in java. I can run both codes in my PC and can connect client and server. But how will I connect 2 computers as a client and server. Here are my codes for server and client as follows:

MyServer1

//code for server
import java.io.*;
import java.net.*;

public class MyServer1
{
    ServerSocket ss;
    Socket s;
    DataInputStream dis;
    DataOutputStream dos;
    public MyServer1()
    {
        try
        {
            System.out.println("Server Started");
            ss=new ServerSocket(10);
            s=ss.accept();
            System.out.println(s);
            System.out.println("CLIENT CONNECTED");
            dis= new DataInputStream(s.getInputStream());
            dos= new DataOutputStream(s.getOutputStream());
            ServerChat();
        }
        catch(Exception e)
        {
             System.out.println(e);
        }
    }

    public static void main (String as[])
    {
         new MyServer1();
    }

    public void ServerChat() throws IOException
    {
         String str, s1;
         do
         {
             str=dis.readUTF();
             System.out.println("Client Message:"+str);
             BufferedReader br=new BufferedReader(new   InputStreamReader(System.in));
             s1=br.readLine();
             dos.writeUTF(s1);
             dos.flush();
         }
         while(!s1.equals("bye"));
    }
}

MyClient1

//code for client
import java.io.*;
import java.net.*;

public class MyClient1
{
    Socket s;
    DataInputStream din;
    DataOutputStream dout;
    public MyClient1()
    {
         try
         {
             //s=new Socket("10.10.0.3,10");
             s=new Socket("localhost",10);
             System.out.println(s);
             din= new DataInputStream(s.getInputStream());
             dout= new DataOutputStream(s.getOutputStream());
             ClientChat();
         }
         catch(Exception e)
         {
             System.out.println(e);
         }
     }
     public void ClientChat() throws IOException
     {
           BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
           String s1;
           do
           {
               s1=br.readLine();
               dout.writeUTF(s1);
               dout.flush();
               System.out.println("Server Message:"+din.readUTF());
           }
           while(!s1.equals("stop"));
    }
    public static void main(String as[])
    {
        new MyClient1(); 
    }
}
2
  • 2
    Step 1: format your code so we can read it. Step 2: learn about hostnames. Commented Feb 3, 2015 at 20:58
  • At first, the client should create a socket to your server's ip address, not the client it self (localhost) Commented Feb 3, 2015 at 21:01

3 Answers 3

2

Client Just needs the IP of the Server.
You have to find out the IP of the server and tell the client about it, like:

String serverName = "IP of server comes here";  // Indicating the place to put Server's IP
s = new Socket(serverName, 10);

Server needs no change.

Sign up to request clarification or add additional context in comments.

1 Comment

WAN ip or LAN ip?
0

You have to just enter ip of the server while creating Socket Instance. i suggest you to follow steps

1) start hotspot in any one computer which you going to use as server 2) in second computer start wifi and connect with hotspot which we just started. 3) now ho to sharing center and click on network you connect and check detail and copy dns server ip and paste it in client program

1 Comment

You should add some code to your answer to increase your answer's robustness. For more help with crafting an answer, check out this link.
0

I would like to add that the server ip depends on your type of connection to your client. If they are both in the same local network, you're gonna use the LAN ip address of your server. Otherwise use the WLAN ip address. I would strongly recommend that you set manually (static) both server and client ip addresses.

1 Comment

This doesn't really answer the question. To me, it makes more sense as a comment on whatever you're trying to add to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.