java連線伺服器
阿新 • • 發佈:2019-01-24
Server.java
package com.lisa.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.String;
public class Server {
private ServerSocket server;
/**
*
*/
public static void main(String[] args) {
Server server = new Server();
server.start();
}
/**
* start
*/
public void start() {
try {
server = new ServerSocket(8888);
// be ready to receive
this.receive();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* receive
*/
private void receive() {
try {
Socket client = server.accept();
StringBuilder sb = new StringBuilder();
// receive the message from client
String msg = null;
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
// assign msg
while ((msg = br.readLine()).length() > 0) {
sb.append(msg);
sb.append("\r\n");
if(msg == null) {
break;
}
}
// delect the blank
String requestInfo = sb.toString().trim();
System.out.println(requestInfo);
} catch (IOException e){
}
}
/**
* stop
*/
public void stop() {
}
}
以上即HTTP協議