Java Socket和Server Socket多线程编程

摘要

java Socket和Server Socket多线程编程

下面的代码是java socket客户端和服务端的源码程序


package net.tcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread
{
    ServerSocket server = null;
    Socket sk = null;
    BufferedReader rdr = null;
    PrintWriter wtr = null;
    public Server()
    {
        try
        {
            server = new ServerSocket(1987);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public void run()
    {
        while (true)
            {
                System.out.println("Listenning...");
                try
                {
//                  每个请求交给一个线程去处理
                    sk = server.accept();
                    ServerThread th = new ServerThread(sk);
                    th.start();
                    sleep(1000);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                   
            }
    }
    public static void main(String [] args)
    {
        new Server().start();
    }
       
       
       
       
       
    class ServerThread extends Thread
    {
        Socket sk = null;
        public ServerThread(Socket sk)
        {
            this.sk = sk;
        }
        public void run()
        {
            try
            {
                wtr = new PrintWriter(sk.getOutputStream());
                rdr = new BufferedReader(new InputStreamReader(sk
                        .getInputStream()));
                String line = rdr.readLine();
                System.out.println("从客户端来的信息:" + line);
//              特别,下面这句得加上     “\n”,
                wtr.println("你好,服务器已经收到您的信息!'" + line + "'\n");
                wtr.flush();
                System.out.println("已经返回给客户端!");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
               
        }
           
    }
}
客户端:
Java代码 
1.package net.tcp;  
2. 
3.import java.io.BufferedReader;  
4.import java.io.BufferedWriter;  
5.import java.io.InputStreamReader;  
6.import java.io.OutputStreamWriter;  
7.import java.io.PrintWriter;  
8.import java.net.Socket;  
9. 
10.public class Client extends Thread  
11.{  
12. 
13.    Socket sk = null;  
14.    BufferedReader reader = null;  
15.    PrintWriter wtr = null;  
16.    BufferedReader keyin = null;  
17. 
18.    public Client()  
19.    {  
20.        keyin = new BufferedReader(new InputStreamReader(System.in));  
21.        try 
22.        {  
23.            sk = new Socket("127.0.0.1", 1987);  
24.        }  
25.        catch (Exception e)  
26.        {  
27.            e.printStackTrace();  
28.        }  
29. 
30.    }  
31. 
32.    public void run()  
33.    {  
34.        try 
35.        {  
36.            reader = new BufferedReader(new InputStreamReader(sk  
37.                    .getInputStream()));  
38.            wtr = new PrintWriter(sk.getOutputStream());  
39.            String get = keyin.readLine();  
40. 
41.            while (true)  
42.            {  
43.                if (get != null & get.length() > 0)  
44.                {  
45.                    wtr.println(get);  
46.                    wtr.flush();  
47.//                  wtr.close();  
48.//                  System.out.println(get + "发送完毕");  
49.                }  
50.                if (reader != null)  
51.                {  
52.                    String line = reader.readLine();  
53.                    System.out.println("从服务器来的信息:" + line);  
54.                      
55.                }  
56.                  
57.            }  
58.        }  
59.        catch (Exception e)  
60.        {  
61.            e.printStackTrace();  
62.        }  
63.    }  
64. 
65.    public static void main(String [] args)  
66.    {  
67.        new Client().start();  
68.    }  
69.} 


IT家园
IT家园

网友最新评论 (0)