Join us on Facebook

Please wait..10 Seconds Cancel

1.03.2014

// // Leave a Comment

Create Client‐Server application using Socket class in Java.

Client‐Server application using Socket class

Simple client

import java.io.*; 
import java.net.*;     
 
public class SimpleClient  
{ 
    public static void main(String[] a) 
    { 
        try 
        { 
            Socket s1=new Socket("192.168.1.117",1567); 
            InputStream is=s1.getInputStream(); 
            BufferedReader br=new BufferedReader(new InputStreamReader(is));             
            int i; 
            System.out.println(""); 
 
            while((i=br.read())!=-1) 
            {                 
                System.out.print((char)i);                 
            } 
             
            br.close(); 
            s1.close(); 
        } 
        catch(IOException e) 
        { 
            e.printStackTrace(); 
            System.out.println("Conncection Problem"); 
        } 
    } 
 
     
} 
 

Simple Server

 
import java.net.*; 
import java.io.*; 
 
public class SimpleServer  
{ 
    public static void main(String[] a) 
    { 
        ServerSocket s=null; 
        try 
        { 
            s=new ServerSocket(1567); 
        } 
        catch(IOException e) 
        { 
            e.printStackTrace(); 
        }  
        while(true) 
        { 
            try 
            { 
                Socket s1=s.accept(); 
                OutputStream out=s1.getOutputStream(); 
                BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(out)); 
                 
                bw.write("Hello net World!\n"); 
                 
                bw.close(); 
                s1.close(); 
            } 
            catch(IOException e) 
            { 
                e.printStackTrace(); 
            } 
        } 
    }     
} 

0 comments:

Post a Comment