[java]代码库
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package abc;
import java.net.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class SampleServer {
public static void main(String[] arges) {
try {
int port = 8688;
//使用8888端口创建一个ServerSocket
ServerSocket mySocket = new ServerSocket(port);
//等待监听是否有客户端连接
Socket sk = mySocket.accept();
//输入缓存
BufferedReader in = new BufferedReader(
new InputStreamReader(sk.getInputStream()));
//输出缓存
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
sk.getOutputStream())), true);
//打印接收到的客户端发送过来的信息
System.out.println("客户端信息:" + in.readLine());
System.out.println("String:" + in.toString());
//向客户端回信息
out.println("你好,我是服务器。我使用的端口号: " + port);
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public String toString() {
return "SampleServer{" + '}';
}
}