หน้าเว็บ

วันศุกร์ที่ 15 มิถุนายน พ.ศ. 2555

java socket



Server Socket
package com.blogspot.na5cent.serverSocket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author redcrow
 */
public class MyServerSocket {

    private static final int SERVER_POTH = 8888;

    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            server = new ServerSocket(SERVER_POTH);
            System.out.println("wating for connect...");
            while (true) {
                Socket sock = server.accept();
                System.out.println("client connected");
                System.out.println(sock.getInetAddress());
                System.out.println(sock.getPort());
                System.out.println("------------------------------");
                new Thread(new listener(sock)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                server.close();
            } catch (IOException ex) {
                Logger.getLogger(MyServerSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

class listener implements Runnable {

    private Socket sock;
    private DataInputStream inputStream = null;
    private DataOutputStream outputStream = null;

    public listener(Socket sock) {
        this.sock = sock;
    }

    @Override
    public void run() {
        try {
            while (true) {
                inputStream = new DataInputStream(sock.getInputStream());
                outputStream = new DataOutputStream(sock.getOutputStream());
                outputStream.writeUTF("Hello client, i am server");
                outputStream.flush();
                System.out.println(inputStream.readUTF());
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(listener.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(listener.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                sock.close();
                inputStream.close();
                outputStream.close();
                System.out.println("resource closed");
            } catch (IOException ex) {
                Logger.getLogger(listener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


Client Socket
package com.blogspot.na5cent.clientSocket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author redcrow
 */
public class MyClientSocket {

    private static final int SERVER_POTH = 8888;
    private static final String SERVER_IP = "localhost";
    private static Socket sock;
    private static DataInputStream inputStream = null;
    private static DataOutputStream outputStream = null;

    public static void main(String[] args) {
        try {
            sock = new Socket(SERVER_IP, SERVER_POTH);
            System.out.println(sock.getInetAddress());
            System.out.println(sock.getPort());
            System.out.println("------------------------------");
            while (true) {
                inputStream = new DataInputStream(sock.getInputStream());
                outputStream = new DataOutputStream(sock.getOutputStream());
                System.out.println(inputStream.readUTF());
                outputStream.flush();
                outputStream.writeUTF("Hello server, i am client");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(MyClientSocket.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                sock.close();
                inputStream.close();
                outputStream.close();
                System.out.println("resource closed");
            } catch (IOException ex) {
                Logger.getLogger(MyClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น