集册 Java实例教程 定义到服务器的网络连接

定义到服务器的网络连接

欢马劈雪     最近更新时间:2020-01-02 10:19:05

462
定义到服务器的网络连接

import java.io.BufferedReader;
/* 
 来自 
*n o w j a   v  a . c o m - 时  代  Java*/

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;


public class Main {

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

    final int httpd = 1234;

    ServerSocket ssock = null;


    ssock = new ServerSocket(httpd);

    System.out.println("have opened port 1234 locally");


    Socket sock = ssock.accept();

    System.out.println("client has made socket connection");

    communicateWithClient(sock);

    System.out.println("closing socket");

    ssock.close();
    /* 
     来自 
    *时代Java公众号*/

  }


  public static void communicateWithClient(Socket socket) throws Exception {

    BufferedReader in = null;

    PrintWriter out = null;

    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    out = new PrintWriter(socket.getOutputStream(), true);

    String s = null;

    out.println(
展开阅读全文