TCP Demo 4年前

编程语言
563
TCP Demo

//客户端

import java.io.*;
import java.net.*;

public class Myclient {

    public static void main(String[] args) {
        
        Socket client= null;
        BufferedWriter out = null;
        
        try {
            client = new Socket(InetAddress.getLocalHost(),5555);
            
            out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            out.write("11111111111");
            out.flush();
            
        } catch (IOException e) {
            
            e.printStackTrace();
        }finally {
            
            try {
                out.close();
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }
            
            try {
                client.close();
            } catch (IOException e) {
                
                e.printStackTrace();
            }
        }
        
    }

}

//客户端

import java.io.*;
import java.net.*;

public class Myservice {

    public static void main(String[] args) {
        ServerSocket server= null;
        Socket client = null;
        BufferedReader in = null;
        try {
            server = new ServerSocket(5555);
            client = server.accept();
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            System.out.println(in.readLine());
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(in!=null)
                in.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
            try {
                if(client!=null)
                client.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
            try {
                if(server!=null)
                server.close();
            } catch (IOException e) {
            
                e.printStackTrace();
            }
        }
        

    }

}
image
-暮兮-
波暖绿粼粼,燕飞来,好是苏堤才晓。
2
发布数
0
关注者
1389
累计阅读

热门教程文档

Gin
17小节
Javascript
24小节
Swift
54小节
Typescript
31小节
QT
33小节
广告