Ads 468x60px

##EasyReadMore##

24 3月, 2016

Java 實現 Socket 回覆


Server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ecs.ces.ruleengine;

import java.io.*;
import java.net.*;
import java.lang.Thread;

public class RSocketServer implements Runnable{
 private boolean OutServer = false;
 private ServerSocket server;
 private final int ServerPort = 8591;// open port for listen

 public RSocketServer() {
  try {
   server = new ServerSocket(ServerPort);
   System.out.println("RSocket Server open! port:"+ServerPort);

  } catch (java.io.IOException e) {
   System.out.println("RSocket build error!!");
   System.out.println("IOException"+e.toString());
  }
 }

 public void run() {
  Socket socket;
  
  while (!OutServer) {
   socket = null;
   try {
    synchronized (server) { //指同一時間只能讓一個線程執行或存取
     socket = server.accept();
    }
    System.out.println("Client IP = " + socket.getInetAddress());
    // TimeOut
    socket.setSoTimeout(15000);//丟出SocketTimeoutExcpetion的,有找到Host但一直等不到回應。可以設socket.setSoTimeout(timeout)。

    OutputStream out = socket.getOutputStream();
             InputStream in = socket.getInputStream();
    byte[] inbuffer = new byte[1024];
    String data = "";
    int length;
    
    if ((length = in.read(inbuffer)) > 0) { // socket input
     data += new String(inbuffer,0,length);
     System.out.println("get content = "+data);
     out.write("1".getBytes());
    }
    
    socket.close();
    System.out.println("RSocket close");    

   } catch (java.io.IOException e) {
    System.out.println("Socket exception !");
    System.out.println("IOException :" + e.toString());
   }
  }
 }
 
 public static void main(String[] args){
  Thread thread = new Thread(new RSocketServer());
  thread.start();
 }
}
Client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.ecs.ces.ruleengine;

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

public class MSocketClient{
 private String address = "127.0.0.1";
 private int port = 8591;
 
 public MSocketClient(){
  try{
   //init socket
   Socket client = new Socket(address, port);
   client.setSoTimeout(15000);
   OutputStream out = client.getOutputStream();
            InputStream in = client.getInputStream();

   System.out.println("send msg to server");
   out.write("hello i am MClient msg".getBytes());
   out.flush();
   
   System.out.println("recieve server state msg");
   byte[] inbuffer = new byte[1024];
   int length;
   String data = "";
   if((length = in.read(inbuffer)) > 0){//很多範例會在這邊用 while ,但因為 Java 會是一次讀完,所以在這邊用 while 沒意義,而且會被卡住
    data += new String(inbuffer,0,length);
    System.out.println("client get content = "+data);
   }
   
   //server state check
   int errorCode;
   errorCode = Integer.valueOf(data);
   switch(errorCode){
    case 0:
     System.out.println("ack ok");
    break;
    case 1:
     System.out.println("Error! Message Q is full");
    break;
    default:
     System.out.printf("Error! code = %d\n",errorCode);
    break;
     
   }
   
   System.out.println("client socket close");
   out.close();
   in.close();
   client.close();
   client = null;
  } catch(java.io.IOException e){
   System.out.println("MSocketClient connect error!");
   System.out.println("IOException"+e.toString());
  }
 }
 
 public static void main(String[] args){
  new MSocketClient();
 }
}

[Java]簡單的Socket Client Server | 聰明的生活2 - http://goo.gl/wpRFw

0 意見:

張貼留言

 
Blogger Templates