1. 程式人生 > >java連線伺服器

java連線伺服器

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() { } }

重新整理localhost後後臺介面

以上即HTTP協議