[心得]Java版UDP和TCP C-S代码

UDPClient

套接字,主要使用类DatagramSocket和DatagramPacket

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * Created by topcoder on 2016/10/1.
 */
public class UDPClient {
    public static void main(String args[]){
        //args give message contents and server hostname
        DatagramSocket aSocket = null;
        try{
            aSocket = new DatagramSocket();
            byte [] m = args[0].getBytes();
            InetAddress aHost = InetAddress.getByName(args[1]);
            int serverPort = 6789;
            DatagramPacket request = new DatagramPacket(m, args[0].length(),aHost, serverPort);
            aSocket.send(request);
            byte [] buffer = new byte[1000];
            DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
            aSocket.receive(reply);
            System.out.println("Reply: " + new String(reply.getData()));
        } catch (SocketException e) {
            System.out.println("Socket: " + e.getMessage());
        } catch (IOException e){
            System.out.println("IO: " + e.getMessage());
        } finally {
            if(aSocket != null){
                aSocket.close();
            }
        }
    }
}

UDPServer

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created by topcoder on 2016/10/1.
 */
public class UDPServer {
    public static void main(String args[]){
        DatagramSocket aSocket = null;
        try{
            aSocket = new DatagramSocket(6789);
            byte[] buffer = new byte[1000];
            while(true){
                DatagramPacket request = new DatagramPacket(buffer, buffer.length);
                aSocket.receive(request);
                DatagramPacket reply = new DatagramPacket(request.getData(),
                        request.getLength(), request.getAddress(), request.getPort());
                aSocket.send(reply);
            }
        }catch (SocketException e){
            System.out.println("Socket: " + e.getMessage());
        }catch (IOException e){
            System.out.println("IO: " + e.getMessage());
        }finally{
            if(aSocket != null){
                aSocket.close();
            }
        }
    }
}

TCPClient

使用类DataInputStream

import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.UnexpectedException;

/**
 * Created by topcoder on 2016/10/1.
 */
public class TCPClient {
    public static void main(String[] args){
        //arguments spply message and hostname of destination
        Socket s = null;
        try{
            int serverPort = 8888;
            s = new Socket(args[1], serverPort);
            DataInputStream in = new DataInputStream(s.getInputStream());
            DataOutputStream out =
                    new DataOutputStream(s.getOutputStream());
            out.writeUTF(args[0]);
            String data = in.readUTF();
            System.out.println("Received: " + data);
        }catch (UnknownHostException e){
            System.out.println("Sock: " + e.getMessage());
        }catch (IOException e){
            System.out.println("IO: " + e.getMessage());
        }finally {
            if(s!= null){
                try{
                    s.close();
                }catch (IOException e){
                    System.out.println("close failed");
                }
            }
        }
    }
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * Created by topcoder on 2016/10/1.
 */
public class TCPServer {
    public void main(String[] args){
        try{
            int serverPort = 8888;
            ServerSocket listenSocket = new ServerSocket(serverPort);
            while(true){
                Socket clientSocket = listenSocket.accept();
                Connection c = new Connection(clientSocket);
            }
        }catch (IOException e){
            System.out.println("Listen: " + e.getMessage());
        }
    }

    class Connection extends Thread {
        DataInputStream in;
        DataOutputStream out;
        Socket clientSocket;
        public Connection(Socket aClientSocket){
            try{
                clientSocket = aClientSocket;
                in = new DataInputStream(clientSocket.getInputStream());
                out = new DataOutputStream(clientSocket.getOutputStream());
                this.start();
            }catch (IOException e){
                System.out.println("Connection: " + e.getMessage());
            }
        }
        public void run(){
            try{
                String data = in.readUTF();
                out.writeUTF(data);
            }catch (EOFException e){
                System.out.println("EOF: " + e.getMessage());
            }catch (IOException e){
                System.out.println("IO: " + e.getMessage());
            }finally {
                try{
                    clientSocket.close();
                }catch (IOException e){
                    System.out.println("Close failed");
                }
            }
        }
    }
}

你可能感兴趣的:(技术笔记)