简单的通信(一)----使用Socket实现TCP协议

功能

使用Socket实现TCP协议,分为Client和Server,客户端向服务器端发送消息。

Client端代码

package com.demo;

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


/**
 * 
 * @author Lynn
 *  使用Socket类实现TCP协议; Socket常用的构造方法:Socket(InetAddress address, int
 *  port)和 Socket(String host, int port) 客户端给服务端发送信息,服务端输出此信息到控制台上;
 *
 */
public class Demo02 {
    // 客户端;
    
    public static void main(String[] args) {
        InetAddress inet;
        Socket socket = null;
        OutputStream out = null;
        try {
            inet = InetAddress.getLocalHost();
            //这里填写服务器端的ip和端口号;
            socket = new Socket(inet, 6868);
            out = socket.getOutputStream();
            out.write("我是客户端".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(out!=null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    
}

Server端代码

package com.demo;

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

public class Demo03 {
    // 服务器端;
    public static void main(String[] args){
            //端口号;
            ServerSocket serverSocket=null;
            Socket socket =null;
            InputStream in = null;
            try {
                serverSocket = new ServerSocket(6868);
                socket = serverSocket.accept();
                in = socket.getInputStream();
                byte[] content = new byte[20];
                int length;//记录真正数据的长度;
                while ((length = in.read(content)) != -1) {
                    String str = new String(content, 0, length);
                    System.out.println(str);
                } 
                
            } catch (Exception e) {
                // TODO: handle exception
            }finally {
                if(in!=null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(serverSocket!=null) {
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            
        }

}

运行结果

需要先启动server端代码,再启动client端代码
709915-20181215150415570-460311940.png

转载于:https://www.cnblogs.com/SnailsRunning/p/10123434.html

你可能感兴趣的:(简单的通信(一)----使用Socket实现TCP协议)