code example of socket

Server side (ServiceServer):

package cn.itcast.bigdata.socket;



import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class ServiceServer {


public static void main(String[] args) throws Exception {


// Create a serversocket and bind it to this machine ServerSocket server on port 8899
= new ServerSocket();
server.bind(new InetSocketAddress("localhost", 8899));


// Accept the client's connection request; accept is a blocking method that will wait until a client requests a connection just return
while (true) {
Socket socket = server.accept();
new Thread(new ServiceServerTask(socket)).start();
}

}

}


client():

package cn.itcast.bigdata.socket;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;


public class ServiceClient {


public static void main(String[] args) throws Exception { /*ServiceIterface service = ProxyUtils.getProxy(ServiceIterface.class,"methodA",hostname,port); Result = service.methodA(parameters);*/ // 向服务器发出请求建立连接 Socket socket = new Socket("localhost", 8899); // 从socket中获取输入输出流 InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream();











PrintWriter pw = new PrintWriter(outputStream);
pw.println("hello");
pw.flush();


BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String result = br.readLine();
System.out.println(result); inputStream.close(); outputStream.close(); socket.close(); }







}


ServiceServerTask

package cn.itcast.bigdata.socket;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter ;
import java.net.Socket;


public class ServiceServerTask implements Runnable{
Socket socket ;
InputStream in=null;
OutputStream out = null; public ServiceServerTask(Socket socket) { this.socket = socket; } //Business logic: with the client Data interaction @Override public void run() { try { //Get the input and output stream of network communication with the client from the socket connection  in = socket.getInputStream(); out = socket.getOutputStream();















BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read the data sent by the client from the network communication input stream
//Note: The methods of reading data from socketinputstream are all blocking 
String param = br.readLine( ); /** * Job: * Write the following business invocation logic into a more general one: it can be flexibly invoked according to the invocation class name, invocation method name, and invocation parameter sent by the client * "Reflection" */ GetDataServiceImpl getDataServiceImpl = new GetDataServiceImpl(); String result = getDataServiceImpl.getData(param); //Write the call result to the sokect's output stream to send to the client PrintWriter pw = new PrintWriter(out); pw.println(result); pw.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ try { in.close(); out.close(); socket.close();






















 






} catch (IOException e) {
e.printStackTrace();
}}}






}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325837366&siteId=291194637
Recommended