此程序是应用java中的网络套接字实现的对服务器的链接,利用多线程来实现多个客户端的接入
package com.ltb.server;
import java.io.*;import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class ReceiveServer {
private final int RECEIVE_PORT=9090;
public ReceiveServer(){
ServerSocket ss;
try {
ss = new ServerSocket(RECEIVE_PORT);
System.out.println("welcome to the Server"+new Date()+"the Server is read");
while(true){
Socket s=ss.accept();
Thread serverthread=new ServerThread(s);
serverthread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ReceiveServer();
}
}
此类属于内部类
class ServerThread extends Thread{Socket s;
BufferedReader input;
PrintWriter output;
public ServerThread(Socket s) {
this.s = s;
InputStreamReader read;
OutputStreamWriter writer;
try {
read=new InputStreamReader(s.getInputStream());
writer=new OutputStreamWriter(s.getOutputStream());
input=new BufferedReader(read);
output=new PrintWriter(writer);
} catch (IOException e) {
e.printStackTrace();
}
output.println("welcom to the server");
output.println("time"+new Date()+"Port"+s.getLocalPort());
output.println("whate cann I do for you ?");
}
public void run() {
String cmd=null;
boolean boon=false;
while(!boon){
try {
cmd=input.readLine();
if(cmd!=null&&cmd.equalsIgnoreCase("quit")){
boon=true;
}else if(cmd.startsWith("HELP")){
output.println("");
output.println("quer");
output.println("pwd");
output.println("cd");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}