文本浏览器

Java简单Socket服务端/客户端通讯

发布者 : 管理员-Adler | 发布时间 : 2019-01-17 19:16:03
文章号 : 23 | 阅读量 : 17+1 | AAW值(?) : 0.06 (仅供参考)

服务端

import java.io.DataInputStream;
import 
java.io.DataOutputStream;
import 
java.io.IOException;
import 
java.net.ServerSocket;
import 
java.net.Socket;
import 
java.net.SocketTimeoutException;

public class 
server extends Thread {
    
privateServerSocket ss;

    public 
server (int port) throws IOException {
        
ss new ServerSocket(port);
        
ss.setSoTimeout(1000);
    
}

    
public void run() {
        
while (true) {
            
try {
                System.
out.println("等待远程连接...通过监听端口号: " ss.getLocalPort());
                
Thread.sleep(9000);
                
Socket server = ss.accept();
                
System.out.println("远程主机地址: " + server.getRemoteSocketAddress());
                
DataInputStream in = newDataInputStream(server.getInputStream());
                
System.out.println("来自+ server.getRemoteSocketAddress() + "的请求: " + in.readUTF());
                
DataOutputStream out = newDataOutputStream(server.getOutputStream());
                
out.writeUTF("本服务器: " + server.getLocalAddress() + "正在对您断开连接!\n再见!");
                
server.close();
            
catch (SocketTimeoutException s) {
                System.
out.println("连接超时!");
            
catch (IOException e) {
                e.printStackTrace()
;
                break;
            
catch (InterruptedException i) {
                i.printStackTrace()
;
                break;
            
}
        }
    }

    
public static void main (String[] args) {
        
int port = Integer.parseInt(args[0]);
        try 
{
            Thread t = 
new server(port);
            
//Thread t2 = new server(port);
            
t.run();
            
//t2.run();
        
catch (IOException e) {
            e.printStackTrace()
;
        
}
    }
}

客户端

import java.io.*;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.net.SocketAddress;



public class client{

    public static void main (String[] args) throws IOException, ArrayIndexOutOfBoundsException {

        String serverNM = args[0];

        int port = Integer.parseInt(args[1]);

        System.out.println("正在尝试连接到" + serverNM + ":" + port);

        Socket client = new Socket(serverNM, port);

        System.out.println("成功连接到: " + client.getRemoteSocketAddress());

        OutputStream out2server = client.getOutputStream();

        DataOutputStream out = new DataOutputStream(out2server);

        //发送UTF8字符串         out.writeUTF("你好" + client.getRemoteSocketAddress() + ", 这里是" + client.getLocalAddress() + ". 连接已经成功建立.");

        InputStream inFserver = client.getInputStream();

        DataInputStream in = new DataInputStream(inFserver);

        System.out.println("来自" + client.getRemoteSocketAddress() + "的回应: " + in.readUTF());

        client.close(); //客户端关闭连接     }

}

运行结果

服务端

C:\Users\Adler-WTG\Desktop\webcoding\src>java server 6666

等待远程连接...通过监听端口号: 6666

远程主机地址: /127.0.0.1:51754

来自/127.0.0.1:51754的请求你好localhost/127.0.0.1:6666, 这里是/127.0.0.1. 连接已经成功建立.

等待远程连接...通过监听端口号: 6666

远程主机地址: /127.0.0.1:51756

来自/127.0.0.1:51756的请求你好localhost/127.0.0.1:6666, 这里是/127.0.0.1. 连接已经成功建立.

等待远程连接...通过监听端口号: 6666

连接超时!

客户端

C:\Users\Adler-WTG\Desktop\webcoding\src>java client localhost 6666

正在尝试连接到localhost:6666

成功连接到: localhost/127.0.0.1:6666

来自localhost/127.0.0.1:6666的回应本服务器: /127.0.0.1正在对您断开连接!

再见!






评论加载中...

+ 参与讨论