java socket编程实例

 这篇文章将为你介绍java的socket编程(Java socket programming),服务器端等待客户端的链接。客户端发起连接之后,可以发送数据。在这个例子中,客户端发送一句"Hi my server"。要想终止本次连接,客户端发送"bye"给服务器,服务器将会回发一个“bye”,然后连接变可以终止了。服务器端继续等待下一个连接。这两个程序要在同一个机器上运行。如果你想要在两个不同的机器上运行,你也只需要修改地址“localhost”为服务器端程序所在机器的ip即可。

 

 

 

 

 

 

 

 

 

  1. //服务器端  
  2.   
  3. import java.io.*;  
  4. import java.net.*;  
  5. public class Provider{  
  6.     ServerSocket providerSocket;  
  7.     Socket connection = null;  
  8.     ObjectOutputStream out;  
  9.     ObjectInputStream in;  
  10.     String message;  
  11.     Provider(){}  
  12.     void run()  
  13.     {  
  14.         try{  
  15.             //1. creating a server socket  
  16.             providerSocket = new ServerSocket(200410);  
  17.             //2. Wait for connection  
  18.             System.out.println("Waiting for connection");  
  19.             connection = providerSocket.accept();  
  20.             System.out.println("Connection received from " + connection.getInetAddress().getHostName());  
  21.             //3. get Input and Output streams  
  22.             out = new ObjectOutputStream(connection.getOutputStream());  
  23.             out.flush();  
  24.             in = new ObjectInputStream(connection.getInputStream());  
  25.             sendMessage("Connection successful");  
  26.             //4. The two parts communicate via the input and output streams  
  27.             do{  
  28.                 try{  
  29.                     message = (String)in.readObject();  
  30.                     System.out.println("client>" + message);  
  31.                     if (message.equals("bye"))  
  32.                         sendMessage("bye");  
  33.                 }  
  34.                 catch(ClassNotFoundException classnot){  
  35.                     System.err.println("Data received in unknown format");  
  36.                 }  
  37.             }while(!message.equals("bye"));  
  38.         }  
  39.         catch(IOException ioException){  
  40.             ioException.printStackTrace();  
  41.         }  
  42.         finally{  
  43.             //4: Closing connection  
  44.             try{  
  45.                 in.close();  
  46.                 out.close();  
  47.                 providerSocket.close();  
  48.             }  
  49.             catch(IOException ioException){  
  50.                 ioException.printStackTrace();  
  51.             }  
  52.         }  
  53.     }  
  54.     void sendMessage(String msg)  
  55.     {  
  56.         try{  
  57.             out.writeObject(msg);  
  58.             out.flush();  
  59.             System.out.println("server>" + msg);  
  60.         }  
  61.         catch(IOException ioException){  
  62.             ioException.printStackTrace();  
  63.         }  
  64.     }  
  65.     public static void main(String args[])  
  66.     {  
  67.         Provider server = new Provider();  
  68.         while(true){  
  69.             server.run();  
  70.         }  
  71.     }  
  72. }  
  73.     
  74. //客户端  
  75.   
  76. import java.io.*;  
  77. import java.net.*;  
  78. public class Requester{  
  79.     Socket requestSocket;  
  80.     ObjectOutputStream out;  
  81.     ObjectInputStream in;  
  82.     String message;  
  83.     Requester(){}  
  84.     void run()  
  85.     {  
  86.         try{  
  87.             //1. creating a socket to connect to the server  
  88.             requestSocket = new Socket("localhost"2004);  
  89.             System.out.println("Connected to localhost in port 2004");  
  90.             //2. get Input and Output streams  
  91.             out = new ObjectOutputStream(requestSocket.getOutputStream());  
  92.             out.flush();  
  93.             in = new ObjectInputStream(requestSocket.getInputStream());  
  94.             //3: Communicating with the server  
  95.             do{  
  96.                 try{  
  97.                     message = (String)in.readObject();  
  98.                     System.out.println("server>" + message);  
  99.                     sendMessage("Hi my server");  
  100.                     message = "bye";  
  101.                     sendMessage(message);  
  102.                 }  
  103.                 catch(ClassNotFoundException classNot){  
  104.                     System.err.println("data received in unknown format");  
  105.                 }  
  106.             }while(!message.equals("bye"));  
  107.         }  
  108.         catch(UnknownHostException unknownHost){  
  109.             System.err.println("You are trying to connect to an unknown host!");  
  110.         }  
  111.         catch(IOException ioException){  
  112.             ioException.printStackTrace();  
  113.         }  
  114.         finally{  
  115.             //4: Closing connection  
  116.             try{  
  117.                 in.close();  
  118.                 out.close();  
  119.                 requestSocket.close();  
  120.             }  
  121.             catch(IOException ioException){  
  122.                 ioException.printStackTrace();  
  123.             }  
  124.         }  
  125.     }  
  126.     void sendMessage(String msg)  
  127.     {  
  128.         try{  
  129.             out.writeObject(msg);  
  130.             out.flush();  
  131.             System.out.println("client>" + msg);  
  132.         }  
  133.         catch(IOException ioException){  
  134.             ioException.printStackTrace();  
  135.         }  
  136.     }  
  137.     public static void main(String args[])  
  138.     {  
  139.         Requester client = new Requester();  
  140.         client.run();  
  141.     }  
  142. }  

 


第零空间(http://www.zeroplace.cn)版权所有,转载请注明出处: https://www.zeroplace.cn/article.asp?id=767 谢谢合作

文章来自: 本站原创
Tags:
评论: 0 | 查看次数: 10198