0

Here is an sample TCP/IP Server and Client programms

TCPServer

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

    class TCPServer
    {
       public static void main(String argv[]) throws Exception
          {
             String clientSentence;
             String capitalizedSentence;
             ServerSocket welcomeSocket = new ServerSocket(6789);

             while(true)
             {
                Socket connectionSocket = welcomeSocket.accept();
                BufferedReader inFromClient =
                   new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                clientSentence = inFromClient.readLine();
                System.out.println("Received: " + clientSentence);
                capitalizedSentence = clientSentence.toUpperCase() + '\n';
                outToClient.writeBytes(capitalizedSentence);
             }
          }
    } 

TCPClient

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

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("localhost", 6789);
  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  sentence = inFromUser.readLine();
  outToServer.writeBytes(sentence + '\n');
  modifiedSentence = inFromServer.readLine();
  System.out.println("FROM SERVER: " + modifiedSentence);
  clientSocket.close();
 }
}

In that Client i need to send text hi to Server. Onces the server reads that text Hi from Client side it display Client Id with Text is Active now

Bucks
  • 689
  • 3
  • 11
  • 28
  • 3
    what is your problem actually? – Byter Sep 14 '12 at 05:15
  • i need to get a list of computer connected through lan i got through by looping InetAddress now i need to check wheather that computer is ready for i have to create server and client – Bucks Sep 14 '12 at 05:21
  • Can you elaborate more the question? – David B Sep 14 '12 at 08:41
  • i need to send message form local host to remote i know ip address for the remote connection – Bucks Sep 14 '12 at 10:02
  • @Bucks you have now stated three separate requirements. If you want to loop through InetAddresses there is no evidence of it in your code. Not a real question. – user207421 Sep 14 '12 at 10:18
  • @EJP i completed that parse but i need to send a message form one pc to another pc. i am sure the above code is works in local host but i need to send a message form local host to another computer connected to server – Bucks Sep 14 '12 at 10:27

1 Answers1

0

Use the socket.getInetAddress() to get an InetAddress object including the IP address then call the InetAddress.getHostAddress() to get it as a string.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130