Monday, July 8, 2013

Sending and Receiving Data use Java socket programming

//java socket client example
import java.io.*;
import java.net.*;

public class socket_client
{
    public static void main(String[] args) throws IOException
    {
        Socket s = new Socket();
    String host = "dev.easy-conn.com";
    PrintWriter s_out = null;
    BufferedReader s_in = null;
       
        try
        {
        s.connect(new InetSocketAddress(host , 6557));
        System.out.println("Connected");
           
        //writer for socket
            s_out = new PrintWriter( s.getOutputStream(), true);
            //reader for socket
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        }
       
        //Host not found
        catch (UnknownHostException e)
        {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        }
       
        //Send message to server
    String message = "GET hosts\r\n\r\n";
    s_out.println( message );
           
    System.out.println("Message send");
       
    //Get response from server
    String response;
    while ((response = s_in.readLine()) != null)
    {
        System.out.println( response );
    }
    }
}

1 comment:

  1. 童鞋,您好,关于github scau帐号的事能否给个答复

    ReplyDelete