Java中TCP的简单使用

服务器端:

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();
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值