简易socket客户端和多线程服务器实现
下午时间匆忙写的,尽量简化实现。
客户端:
package
com.XiongYi.client;
import java.io. * ;
import java.net. * ;
public class Client ... {
publicstaticvoidmain(String[]args)throwsIOException...{
finalStringServer_IP="localhost";
finalintServer_Port=1986;
Socketsocket=newSocket(Server_IP,Server_Port);
try...{
System.out.println("ClientStart...");
System.out.println("socket="+socket);
socket.setKeepAlive(true);
PrintWriterout=newPrintWriter(
newBufferedWriter
(newOutputStreamWriter
(socket.getOutputStream())),true);
InputStreamnetIn=socket.getInputStream();
BufferedReaderbufferedReader=newBufferedReader(
newInputStreamReader(System.in));
System.out.print("请输入请求:");
StringhttpString=bufferedReader.readLine();
System.out.println(httpString);
intstick=httpString.indexOf("//");
//找到“HTTP://localhost:80/Tomcat/test.html”中“//”的位置
if(stick!=-1)
httpString=httpString.substring(stick+2);
stick=httpString.indexOf("/");
httpString=httpString.substring(stick);
System.out.println(httpString);
out.println("GET"+httpString+"HTTP/1.1");
out.println("Accept:*/*");
out.println("Accept-Language:zh-cn");
out.println("UA-CPU:x86");
out.println("Accept-Encoding:gzip,deflate");
out.println("User-Agent:R.C.");
out.println("Host:localhost:"+Server_Port);
out.println("Connection:Keep-Alive");
out.flush();
//这里不能用out.close,否则socket也会被关闭!
System.out.println("Readingthepage... ");
bytebuf[]=newbyte[1024];
intnum=0;
while((num=netIn.read(buf,0,buf.length))!=-1)...{
System.out.print(newString(buf,0,num,"GBK"));
}
bufferedReader.close();
netIn.close();
out.close();
}
finally...{
System.out.println("closing..");
socket.close();
}
}
}
import java.io. * ;
import java.net. * ;
public class Client ... {
publicstaticvoidmain(String[]args)throwsIOException...{
finalStringServer_IP="localhost";
finalintServer_Port=1986;
Socketsocket=newSocket(Server_IP,Server_Port);
try...{
System.out.println("ClientStart...");
System.out.println("socket="+socket);
socket.setKeepAlive(true);
PrintWriterout=newPrintWriter(
newBufferedWriter
(newOutputStreamWriter
(socket.getOutputStream())),true);
InputStreamnetIn=socket.getInputStream();
BufferedReaderbufferedReader=newBufferedReader(
newInputStreamReader(System.in));
System.out.print("请输入请求:");
StringhttpString=bufferedReader.readLine();
System.out.println(httpString);
intstick=httpString.indexOf("//");
//找到“HTTP://localhost:80/Tomcat/test.html”中“//”的位置
if(stick!=-1)
httpString=httpString.substring(stick+2);
stick=httpString.indexOf("/");
httpString=httpString.substring(stick);
System.out.println(httpString);
out.println("GET"+httpString+"HTTP/1.1");
out.println("Accept:*/*");
out.println("Accept-Language:zh-cn");
out.println("UA-CPU:x86");
out.println("Accept-Encoding:gzip,deflate");
out.println("User-Agent:R.C.");
out.println("Host:localhost:"+Server_Port);
out.println("Connection:Keep-Alive");
out.flush();
//这里不能用out.close,否则socket也会被关闭!
System.out.println("Readingthepage... ");
bytebuf[]=newbyte[1024];
intnum=0;
while((num=netIn.read(buf,0,buf.length))!=-1)...{
System.out.print(newString(buf,0,num,"GBK"));
}
bufferedReader.close();
netIn.close();
out.close();
}
finally...{
System.out.println("closing..");
socket.close();
}
}
}
多线程服务端:
package
com.XiongYi.server;
import java.io. * ;
import java.net. * ;
public class Server extends Thread ... {
intserver_Port;
publicServer(intsp)...{
server_Port=sp;
}
publicvoidrun()...{
System.out.println("Aserverthreadisrunning...");
try...{
ServerSocketserverSocket=newServerSocket(server_Port);
System.out.println("ServerSocket="+serverSocket);
Socketsocket=serverSocket.accept();
System.out.println("Serverstart...");
System.out.println("Connectionaccepted,thesocketis"+socket);
BufferedReaderin=newBufferedReader(newInputStreamReader(socket
.getInputStream()));
PrintWriterout=newPrintWriter(newBufferedWriter(
newOutputStreamWriter(socket.getOutputStream())),true);
out.println("Hi,I'maserver...");
System.out.println("Readingtherequest...");
StringoutS="Thehttprequest:";
while(outS!=null)...{
System.out.println(outS=in.readLine());
}
out.close();
in.close();
socket.close();
serverSocket.close();
System.out.println("closing...");
}catch(IOExceptione)...{
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)...{
inti=0;
while(i<10)...{
//最多启用10线程
newServer(1986+i).run();
}
}
}
import java.io. * ;
import java.net. * ;
public class Server extends Thread ... {
intserver_Port;
publicServer(intsp)...{
server_Port=sp;
}
publicvoidrun()...{
System.out.println("Aserverthreadisrunning...");
try...{
ServerSocketserverSocket=newServerSocket(server_Port);
System.out.println("ServerSocket="+serverSocket);
Socketsocket=serverSocket.accept();
System.out.println("Serverstart...");
System.out.println("Connectionaccepted,thesocketis"+socket);
BufferedReaderin=newBufferedReader(newInputStreamReader(socket
.getInputStream()));
PrintWriterout=newPrintWriter(newBufferedWriter(
newOutputStreamWriter(socket.getOutputStream())),true);
out.println("Hi,I'maserver...");
System.out.println("Readingtherequest...");
StringoutS="Thehttprequest:";
while(outS!=null)...{
System.out.println(outS=in.readLine());
}
out.close();
in.close();
socket.close();
serverSocket.close();
System.out.println("closing...");
}catch(IOExceptione)...{
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)...{
inti=0;
while(i<10)...{
//最多启用10线程
newServer(1986+i).run();
}
}
}