标准的输入格式

风华正茂、服务器端

选取八线程来贯彻服务器与多顾客端之间的通讯。

InputStreamReader isr=new InputStreamReader(System.in);
  BufferedReader br=new BufferedReader(isr);

1、创制serverSocket,即服务器端的socket,绑定钦定的端口,并侦听此端口

着力步骤:

从Socket中得到的正规输出格式

ServerSocket server = new ServerSocket(8888);

1.劳务器端成立ServerSocket,循环调用accept(卡塔尔等待客户端的总是

 PrintWriter pw=new PrintWriter(s.getOutputStream());//s为Socket对象名

2、调用accept(卡塔尔国方法,领头侦听,等待客户端的接连几日,在未连接成功早先,处于窒碍状态,重返的socket,用于与顾客端连接

2.顾客端成立三个socket并央求和劳务器端的连天

从Socket中收获的科公输盘入格式

Socket socket = server.accept();

3.服务器端选择客商端的伸手,创制socket与该客商端创设专线连接

BufferedReader bw=new BufferedReader(new
InputStreamReader(s.getInputStream()));

3、获取输入流,用来读取客商端发来的音讯

4.创建连接的四个socket在二个单独的线程上对话

在截止PrintWriter,BufferedReader,Socket 时都应当展开关闭

InputStream is = socket.getInputStream(卡塔尔(قطر‎;//字节输入流
InputStream里德r isr = new InputStreamReader(is卡塔尔国;//字符输入流
BufferedReader br = new BufferedReader(isr卡塔尔(قطر‎;//缓冲输入流
String info = null;
while((info=br.readLine()) != null){
//循环读取数据
System.out.println(“客商端说:”+info卡塔尔(قطر‎;
}
socket.shutdownInput(卡塔尔;//关闭输入流

5.服务器端继续伺机新的顾客端的接连几天

4、获取输出流,客商响应客商端的恳求

Server.java

OutputStream os = socket.getOutputStream(State of Qatar;//字节出口流
PrintWriter pw = new PrintWriter(os卡塔尔国;//打字与印刷输出流
pw.write(“服务器端已选用你的倡议,允许登入”卡塔尔;
pw.flush();
socket.shutdownOutput(卡塔尔(قطر‎;//关闭输出流

public class Server {

5、关闭能源

public static void main(String[] args) throws IOException{

br.close();
isr.close();
is.close();
pw.close();
os.close();
socket.close();
server.close();

澳门新葡萄京官网注册,//1、创造三个劳动器端的socket,钦点绑定的端口,并侦听此端口

二、客户端

ServerSocket server = new ServerSocket(8888);

1、创建客商端socket,内定要连接的服务器地址和端口号

System.out.println(“********服务端将在开发银行,等待顾客端的连年********”);

Socket socket = new
Socket(“127.0.0.1”,8888);

int count = 0;

2、获取输出流,给服务器端发送音信

//2、开端侦听,等待客户端的连天

OutputStream os = socket.getOutputStream();//字节流
PrintWriter pw = new PrintWriter(os);//打印流
pw.write(“用户名:admin;密码:123”);
pw.flush();
socket.shutdownOutput(卡塔尔;//关闭输出流

while(true){

3、获取输入流,用来读取服务器端发来的新闻

Socket socket = server.accept();

InputStream is = socket.getInputStream(State of Qatar;//字节输入流
InputStreamReader isr = new InputStreamReader(is卡塔尔(قطر‎;//字符输入流
BufferedReader br = new BufferedReader(isrState of Qatar;//缓冲输入流
String info = null;
while((info=br.readLine()) != null){
System.out.println(“服务端说:”+info卡塔尔国;
}
socket.shutdownInput(卡塔尔国;//关闭输入流

ServerThread thread = new   ServerThread(socket);

4、关闭能源

thread.start();

br.close();
isr.close();
is.close();
pw.close();
os.close();
socket.close();

count++;

System.out.println(“顾客端的数据:”+count卡塔尔国;

InetAddress address = socket.getInetAddress();

System.out.println(“客商端的ip:”+address.getHostAddress(卡塔尔卡塔尔国;

}

}

}

ServerThread.java

public class ServerThread extends Thread {

 

//和本线程相关的socket

Socket socket = null;

public ServerThread(Socket socket){

this.socket = socket;

}

//线程实践操作,响应顾客端的央浼

public void run(){

InputStream is = null;

InputStreamReader isr = null;

BufferedReader br = null;

OutputStream os = null;

PrintWriter pw = null;

try {

//3、获取输入流,用来读取顾客端发送的音讯

is = socket.getInputStream(State of Qatar;//字节输入流

isr = new InputStreamReader(is卡塔尔(قطر‎;//字符输入流

br = new Buffered里德r(isrState of Qatar;//缓冲输入流

String info = null;

while((info=br.readLine()) != null){

//循环读取数据

System.out.println(“顾客端说:”+infoState of Qatar;

}

socket.shutdownInput(卡塔尔;//关闭输入流

 

os = socket.getOutputStream(卡塔尔国;//字节出口流

pw = new PrintWriter(os卡塔尔国;//打字与印刷输出流

pw.write(“服务器端已选用你的央求,允许登入”卡塔尔(قطر‎;

pw.flush();

socket.shutdownOutput();

catch (IOException e) {

e.printStackTrace();

finally{

try {

//4、关闭财富

if(pw != null)

pw.close();

if(os != null)

os.close();

if(br != null)

br.close();

if(isr != null)

isr.close();

if(is != null)

is.close();

if(socket != null)

socket.close();

catch (IOException e) {

e.printStackTrace();

}

}

}

}

Client.java

public class Client {

 

public static void main(String[] args) throws UnknownHostException, IOException{

//1、创制顾客端socket,钦赐服务器地址和端口

Socket socket = new Socket(“127.0.0.1”,8888);

System.out.println(“******客商端本来就有备无患伏贴*******”);

//2、给服务端发送数据

OutputStream os = socket.getOutputStream();//字节流

PrintWriter pw = new PrintWriter(os);//打印流

pw.write(“用户名:admin;密码:123456”);

pw.flush();

socket.shutdownOutput(卡塔尔国;//关闭输出流

InputStream is = socket.getInputStream(State of Qatar;//字节输入流

InputStreamReader isr = new InputStreamReader(is卡塔尔;//字符输入流

BufferedReader br = new BufferedReader(isrState of Qatar;//缓冲输入流

String info = null;

while((info=br.readLine()) != null){

System.out.println(“服务端说:”+info卡塔尔(قطر‎;

}

socket.shutdownInput();//关闭输入流

//3、关闭财富

br.close();

isr.close();

is.close();

pw.close();

os.close();

socket.close();

}

}