Sunday, 7 October 2012

Tcp client server communication using Java

TCP Client



import java.io.*;
import java.net.*;
 class tcpclient {
    public static void main(String args[])throws Exception
    {
        Socket s = new Socket("localhost",6700);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintStream ps2 = new PrintStream(s.getOutputStream());
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        String str1,str2;
        while(true)
        {

            str1=kb.readLine();
            ps2.println(str1+"\n");
            str2=br.readLine();
            System.out.println(str2);
           
        }

       
    }
   
}

TCP Server


import java.io.*;
import java.net.*;
import java.lang.*;
class tcpserver {
    public static void main(String args[])throws Exception
    {
        ServerSocket ss = new ServerSocket(6700);
        Socket s = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        PrintStream ps = new PrintStream(s.getOutputStream());

        String str1,str2;
        while(true)
        {
            str1=kb.readLine();
           
               ps.println(str1);
          str2=br.readLine();
                System.out.println(str2+"\n");
               
               
           
        }
       
       
       
       
    }
   
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More