>

Sunday, December 16, 2012

Stream Socket (using TCP) Socket Programming In Java

Stream Socket (using TCP) Socket Programming In Java

Perhaps, before you read this post you better to read my earlier post l link, perhaps you want to know what socket is?, what is socket programming?, and technically for create your code or for your comparing about method socket programming in java. or you just skip it.

Stream Socket(Using TCP)
socket programming using TCP, there is condition we must fulfill first, between server and client must establish connection first once server accept, the client can send message.

Now I will make server class first:



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
  public static void main(String[] args) throws IOException {

   ServerSocket serverSocket = new ServerSocket(3000);
   Socket client = null;

   try {
    System.out.println("waiting for request...");
    while (true) {
      client = serverSocket.accept();
      BufferedReader bufferedReader = new BufferedReader(new 
      InputStreamReader(client.getInputStream()));

      OutputStream outputStream = client.getOutputStream();
      String sentences = bufferedReader.readLine();

      System.out.println("sentences "+sentences.length());

      if(sentences.length()!= 0){
        System.out.println("message from client --- "+
        sentences);

        outputStream.write( ("\nyou have send message to"+  
        "server----"+ sentences).getBytes());

        outputStream.write("End\n".getBytes());
      }
     }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
}


and then I created client


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class TCPClient {
  public static void main(String[] args) throws IOException {

   BufferedReader userInput = new BufferedReader(new 
   InputStreamReader(System.in));

   String sentence = userInput.readLine();

   Socket clientSocket = null;
   OutputStream outputStream = null;
   BufferedReader bufferedReader = null;

   try {
    clientSocket = new Socket("localhost", 3000);
    outputStream = clientSocket.getOutputStream();
    bufferedReader=newBufferedReader(new 
    InputStreamReader(clientSocket.getInputStream()));

    outputStream.write((sentence + "\n").getBytes());
    outputStream.write(("End\n").getBytes());

    String reply;
    while (true) {
      reply = bufferedReader.readLine();
      if (reply.equalsIgnoreCase("End")) {
        break;
       }
      if(reply.length()!= 0){
       System.out.println("you got reply ---"+ reply);
       }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    outputStream.close();
   bufferedReader.close();
   clientSocket.close();
  }
  }
}
both client and server has been created if you want to know technically how to run both class so can communicate each other, and interchange between console you better read my earlier post link.

Firstly I will run the server :
shabbynote run server
Server Run
 
then I run the client 
shabbynote run client
run the client

 
after I wrote in console and I press enter and the console will show like bellow
shabbynote after click enter
after click enter
 
and we check the server console , to see that our message has been sent to server:
shabbynote server response
server response
 
the best thing for using TCP that you won’t lost your package data and the sequence that won’t be unordered like in using datagram/UDP.

No comments:

Post a Comment