简易socket客户端和多线程服务器实现

简易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();
}


}


}

 


多线程服务端:

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


}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值