JAVA 의 scoket 간단 한 사용법

4396 단어
클 라 이언 트
package client;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

import javax.xml.ws.handler.MessageContext.Scope;

public class Client1 {
    private Scanner sc=new Scanner(System.in);
    private OutputStream out;
    private InputStream in;
    private Socket socket;
    public Client1(){
        try {
            //  socket  
            socket = new Socket("127.0.0.1",8888);
            System.out.println("     ...");
            while(true){
                System.out.println("   :");
                
                out = socket.getOutputStream();
                //  
                String next = sc.next();
                
                out.write(next.getBytes());
                
                
                in = socket.getInputStream();
                byte[] b=new byte[1024*1024];
                
                int len = in.read(b);
                
                String string = new String(b, 0, len);
                
                if("bye".equals(string)){
                    break;
                }
                System.out.println("     :"+string);
            }
            in.close();
            out.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Client1 c=new Client1();
    }
}


서버 쪽
package server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server1 {
    
    private Scanner sc=new Scanner(System.in);
    public Server1(){
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("        .....");
            Socket socket = serverSocket.accept();
            System.out.println("    ");
            while(true){
                
                InputStream in = socket.getInputStream();
                
                byte[] b=new byte[1024*1024];
                
                int len = in.read(b);
                
                String id=new String(b,0,len);
                
                System.out.println("    :"+id);
                
                OutputStream out = socket.getOutputStream();
                
                String next = sc.next();
                out.write(next.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Server1 s=new Server1();
    }
}


다 중 스 레 드 서버 엔 드
package more.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerMore {
    public static void main(String[] args) {
        ServerMore s=new ServerMore();
    }
    public ServerMore(){
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("        .....");
            while(true){
                Socket socket = serverSocket.accept();
                System.out.println("    ");
                ServerThread st=new ServerThread(socket);
                new Thread(st).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class ServerThread implements Runnable{

    private Socket socket;
    private InputStream in;
    private OutputStream out;
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            while(true){
                in = socket.getInputStream();
                byte[] b=new byte[1024];
                int len = in.read(b);
                String msg=new String(b,0,len);
                System.out.println("           :\t"+msg);
                if("bye".equals(msg)){
                    break;
                }
                out=socket.getOutputStream();
                out.write("     ".getBytes());
            }
            in.close();
            out.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

좋은 웹페이지 즐겨찾기