score:0

replace

socket clientsocket = new socket("ip of my laptop here", 6789);

with

socket clientsocket = new socket(server address, 6789);

score:0

  1. on your command prompt, type:ipconfig

    this will give the ip address of your machine.

  2. now assuming that you are running the server code on your machine, pass the ip address retrieved from step 1 to the client code (the code run by your friend who is the client):

    socket clientsocket = new socket(<here goes the ip address of the server machine>, 6789);
    

now, whichever machine is running the client code will need to pass on the ip address of the server machine, so that it can connect with each other. so if you want to chat with your friend, after running the server class code, run the client class code too. now both of you are connected to the server and can chat.

score:1

to create client socket you need to use the server's ip address not yours.

package connect1;

import java.io.*; 
import java.net.*;  
import javax.swing.joptionpane;
import java.util.scanner;
class tcpclient { 
public static void main(string argv[]) throws exception
{ 
    string sentence;
    string modifiedsentence;
    bufferedreader infromuser =new bufferedreader(new   inputstreamreader(system.in));
    socket clientsocket = new socket(inetaddress.getbyname("server_ip"), 6789);
    dataoutputstream outtoserver =new dataoutputstream(clientsocket.getoutputstream());
    bufferedreader infromserver =new bufferedreader(new inputstreamreader(clientsocket.getinputstream()));
    system.out.print("enter characters to be capitalized: ");
    sentence =infromuser.readline(); 
    outtoserver.writebytes(sentence + '\n'); 
    modifiedsentence = infromserver.readline(); 
    system.out.println("from server: " + modifiedsentence); 
    scanner input=new scanner(system.in);
    //system.out.print("do you want to enter again? press '0' for 'yes' and '1' if 'no'.");
    //i=input.nextint();

 }
  }

replace "server_ip" by server's ip.


Related Query

More Query from same tag