Java中TCP的简单使用

来源:互联网 发布:js li取消隐藏 编辑:程序博客网 时间:2024/06/02 02:49

服务器端:

import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;/** * Created by cuboo on 2016/10/12. */public class intnet_server {    private ServerSocket serverSocket;    private Socket socket;    private InputStreamReader reader;    private OutputStreamWriter writer;    public static void main(String agrs[]){        getHostInfo();        intnet_server server = new intnet_server();        server.getServer();    }    public static void getHostInfo(){        InetAddress ip;        try {            ip = InetAddress.getLocalHost();            String name = ip.getHostName();            String localip = ip.getHostAddress();            System.out.println("本机名:"+name);            System.out.println("本机IP地址:"+localip);        } catch (UnknownHostException e) {            e.printStackTrace();        }    }    public  void getServer(){        try {            serverSocket = new ServerSocket(8888,10,InetAddress.getLocalHost());            System.out.println("服务器套接字创建成功!");            int i = 0;            while (true){                System.out.println("等待连接..."+i);                i++;                socket = serverSocket.accept();                getClientInfo();                sentMessage();           }        } catch (IOException e) {            e.printStackTrace();        }    }    public  void getClientInfo(){        try {            DataInputStream dis = new DataInputStream(socket.getInputStream());            System.out.println(dis.readUTF());//            reader = new InputStreamReader(socket.getInputStream());//            char[] chars = new char[100];//            reader.read(chars);//            System.out.println(new String(chars,0,chars.length));        } catch (IOException e) {            e.printStackTrace();        }    }    public void sentMessage(){        try {            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());            dos.writeUTF("server");            dos.flush();//            writer = new OutputStreamWriter(socket.getOutputStream());//            writer.write("server");//            writer.flush();        } catch (IOException e) {            e.printStackTrace();        }    }}

客服端:

import java.io.*;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;/** * Created by cuboo on 2016/10/12. */public class client {    private Socket socket;    private InputStreamReader reader;    private OutputStreamWriter writer;    private  DataOutputStream bos;    private DataInputStream dis;    public static void main(String agrs[]){        getHostInfo();        client c = new client();        c.myClient();        c.recieveMessage();    }    public void myClient(){        try {            socket = new Socket("ipv4.cuboo.cn",8888);            bos = new DataOutputStream(socket.getOutputStream());            bos.writeUTF("client");            bos.flush();//            writer = new OutputStreamWriter(socket.getOutputStream());//            writer.write("client");//            writer.flush();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void getHostInfo(){        InetAddress ip;        try {            ip = InetAddress.getLocalHost();            String name = ip.getHostName();            String localip = ip.getHostAddress();            System.out.println("本机名:"+name);            System.out.println("本机IP地址:"+localip);        } catch (UnknownHostException e) {            e.printStackTrace();        }    }    public void  recieveMessage(){        try {            dis = new DataInputStream(socket.getInputStream());            System.out.println(dis.readUTF());//            reader = new InputStreamReader(socket.getInputStream());//            char[] chars = new char[100];//            reader.read(chars);//            System.out.println(new String(chars,0,chars.length));        } catch (IOException e) {            e.printStackTrace();        }    }}


0 0