026.3 网络编程 TCP聊天

分为客户端和服务端,分别进行收发操作
##########################################################################
客户端:
###思路:
1、建立tcp客户端服务
    1.1因为是面向连接,必须有连接才有通信
    1.2在创建客户端时,就必须明确目的地址和端口
2、一旦连接建立,就有了传输数据的通道。就可以在通道中进行数据传输,这个传输是通过流实现的,是socket  io流
3、获取socket  io中的写动作就可以发送给服务端

###步骤:
1、创建socket对象,明确目的IP和端口
2、通过socket对象的getOutStream方法获取OutputStream对象
3、通过OutputStream对象写数据,实现发送
4、关闭socket对象

###代码:
System.out.println("client start");
//1、创建客户端对象,明确目的和端口
Socket s = new Socket("192.168.10.141",10000);
//2、获取socket流中的输出流,将数据发送给服务端
OutputStream out = s.getOutputStream();
//3、通过输出流写数据
out.write("I miss you".getBytes());
//4、关闭资源
s.close();

 

####################################################################
服务端:
###思路:
1、创建socket服务器端服务。服务器监听一个端口
2、获取客户端对象
3、获取客户端的socket流的读取流
4、显示

###步骤:
1、创建ServerSocket服务器对象
2、通过ServerSocket对象的accept方法,获取客户端socket对象
3、通过客户端socket对象的getInetAddress().getHostAddress()   方法获取ip对象然后获取ip
4、通过客户端socket对象的getInputStream方法获取InputStream对象
5、通过InputStream对象的read方法获取客户端发送的数据。
6、关闭客户端socket对象。

###代码:
System.out.println("服务器启动。。。");
//1、创建服务器端对象
ServerSocket ss = new ServerSocket(10003);
//2、获取客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connect");
//3、通过客户端对象获取socket流的读取流
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String str = new String(buf,0,len);
System.out.println(str);

s.close();

 

################################################################################
简单互动的TCP通讯

/*###
 * 案例二:实现客户端和服务端的收发过程。
 * 客户端
 */
System.out.println("客户端2 启动.......");
//        创建客户端socket对象。明确服务端地址和端口。
Socket s = new Socket("192.168.1.223", 10004);
//        发送数据,通过socket输出流完成。
OutputStream out = s.getOutputStream();
out.write("服务端,我来了".getBytes());
//        读取服务端返回的数据,通过socket输入流
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
//        关闭资源。
s.close();

 

/*###
 * 案例二:实现客户端和服务端的收发过程。 服务器端。
 */
System.out.println("服务端2启动.....");
// 创建tcp服务端socket 明确端口。
ServerSocket ss = new ServerSocket(10004);
while (true) {
    // 获取客户端对象。
    Socket s = ss.accept();
    System.out.println(s.getInetAddress().getHostAddress()
            + ".....connected");
    // 读取客户端的发送过来的数据
    InputStream in = s.getInputStream();
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    String text = new String(buf, 0, len);
    System.out.println(text);
    // 给客户端回馈数据。
    OutputStream out = s.getOutputStream();
    out.write("客户端,我已到收到,哦耶!".getBytes());
    // 关闭客户端
    s.close();
}
// 关闭服务端。如果不断的获取客户端,不用关闭服务端。
//        ss.close();

 

###########################################################################
我想写一个可以多次发送信息的程序,没有找到哪里错了

public class TCPServer2
{
    public static void main(String[] args) throws IOException
    {
        ServerSocket ss = new ServerSocket(10005);
        Socket s = ss.accept();
        ServerReceive sr = new ServerReceive(s);
        ServerSend ssend = new ServerSend(s);
        new Thread(sr).start();
        new Thread(ssend).start();
    }
}


class ServerSend implements Runnable{
    private Socket s;
    private OutputStream os;
    public ServerSend(Socket s) throws IOException {
        super();
        this.s = s;
        os = s.getOutputStream();
    }

    @Override
    public void run()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        try {
            while((line = br.readLine()) != null){
                
                os.write(line.getBytes());
                
                if("//over".equals(line)){
                    break;
                }
            }
            s.close();
        } catch (IOException e) {
        }
    }
}

class ServerReceive implements Runnable{
    private Socket s;
    private InputStream is;
    public ServerReceive(Socket s) throws IOException {
        super();
        this.s = s;
        is = s.getInputStream();
    }

    @Override
    public void run()
    {
        while(true){
            byte[] buf = new byte[1024];
            try {
                int len = is.read(buf);
                String str = new String(buf,0,len);
                if("//over".equals(str)){
                    break;
                }
                System.out.println(str);
                s.close();
            } catch (IOException e) {
            }
        }
    }
}
TCPServer
public class TCPClient2
{
    public static void main(String[] args) throws UnknownHostException, IOException
    {
        Socket bothSocket = new Socket("127.0.0.1",10005);
        Send2 send = new Send2(bothSocket);
        Receive2 receive = new Receive2(bothSocket);
        
        new Thread(send).start();
        new Thread(receive).start();
        
    }
}

//创建发送和接收的类,使用多线程,继承runnable接口
//多线程发送类
class Send2 implements Runnable{
    private Socket s;
    OutputStream os;
    public Send2(Socket s) throws IOException {
        super();
        this.s = s;
        os = s.getOutputStream();
    }
    @Override
    public void run()
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        try {
            while((line = br.readLine()) != null){
                
                os.write(line.getBytes());
                
                if("//over".equals(line)){
                    break;
                }
            }
            s.close();
        } catch (IOException e) {
        }
    }
}

//多线程接收类
class Receive2 implements Runnable{
    private Socket s;
    
    private InputStream is;
    public Receive2(Socket s) throws IOException {
        super();
        this.s = s;
    }

    @Override
    public void run()
    {
        while(true){
            byte[] buf = new byte[1024];
            try {
                is = s.getInputStream();
                int len = is.read(buf);
                String str = new String(buf,0,len);
                if("//over".equals(str)){
                    break;
                }
                System.out.println(str);
                s.close();
            } catch (IOException e) {
            }
        }
    }
}
TCPClient

 

posted @ 2018-10-10 19:37  Alos403  阅读(360)  评论(0编辑  收藏  举报