网络

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.io.PrintWriter;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 
 8 public class Server {
 9     public static void main(String[] args) {
10         ServerSocket serverSocket = null;
11         Socket socket = null;
12         try {
13             serverSocket = new ServerSocket(8585);
14             /**
15              * 在服务器端接受客户端的连接,
16              */
17             while(true){
18                 try{
19                     socket = serverSocket.accept();
20                     //System.out.println("A client connect!");
21                     //System.out.println(""+socket.getPort() + socket.getInetAddress());
22                     String name = socket.getInetAddress().getHostName() + " " + socket.getPort();
23                     System.out.println(name);
24                     //通过套接字进行数据传输
25                     BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
26                     String str = br.readLine();
27                     System.out.println(str);
28                     PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
29                     out.println("Receive:" + str);
30                 }finally{
31                     if (socket != null) {
32                         socket.close();
33                     }
34                 }
35             }
36         } catch (Exception e) {
37             e.printStackTrace();
38         }
39         finally{
40             try {
41                 if (serverSocket != null) {
42                     serverSocket.close();
43                 }
44             } catch (Exception e2) {
45                 e2.printStackTrace();
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
//客户端
public class Client {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            /*Creates a stream socket and connects it to the specified port number at the specified IP address.*/
            socket = new Socket("127.0.0.1", 8585);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);//字符输出用PrintWriter
            out.println("Hello World!");
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//接受字符
            String str = br.readLine();
            System.out.print(str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2013-12-07 08:57  soul390  阅读(162)  评论(0编辑  收藏  举报