华子11
2017-03-02 23:50:47浏览 10786
Server.java
package com.zh.socket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 基于TCP协议的socket通信,实现用户登录
* 服务器端
*/
public class Server {
public static void main(String[] args) {
try {
//1.创建服务器端socket,即ServerSocket,并绑定指定的端口,并在端口进行监听
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket = null;
//记录客户端的数量
int count = 0;
System.out.println("***服务器即将启动,等待客户端的连接***");
//循环监听等待客户端的连接
while(true){
//2.通过accept()方法对客户端进行监听,等待客户端的连接
socket=serverSocket.accept();//返回的实例用来与客户端进行通信
ServerThread serverThread = new ServerThread(socket);
//启动线程
serverThread.start();
count++;
System.out.println("客户端数量: "+count);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端的IP: "+address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
package com.zh.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 客户端
*/
public class Client {
public static void main(String[] args){
try {
//1.创建Socket,用于向服务器端发送请求
Socket socket = new Socket("localhost",8888);
//2.创建Socket字节输出流
OutputStream os = socket.getOutputStream();
//3.将字节输出流包装成Print流
PrintWriter pw = new PrintWriter(os);
pw.write("用户名:zh;密码:456");
pw.flush();
socket.shutdownOutput();//关闭输出流
//4.创建输入流接收服务器端响应的数据
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
while((info=br.readLine())!=null){
System.out.println("我是客户端,服务器说: "+info);
}
socket.shutdownInput();//关闭输入流
//5.关闭资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ServerThread.java
package com.zh.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/*
* 服务器线程处理类
*/
public class ServerThread extends Thread {
//和本线程相关的socket
Socket socket = null;
public ServerThread(Socket socket){
this.socket = socket;
}
//线程执行的操作,响应客户端的请求
public void run(){
//获取输入流,并读取客户端的信息
InputStream is = null;
OutputStream os = null;
PrintWriter pw = null;
BufferedReader br = null;
InputStreamReader isr = null;
try {
is = socket.getInputStream();
isr=new InputStreamReader(is);//将字节流包装为字符流
br=new BufferedReader(isr);//为输入流添加缓冲
String info = null;
while((info=br.readLine())!=null){
System.out.println("我是服务器,客户端说: "+info);
}
socket.shutdownInput();//关闭输入流
os = socket.getOutputStream();
pw = new PrintWriter(os);//包装成打印流
pw.write("欢迎您!");
pw.flush();
}catch (IOException e){
e.printStackTrace();
}finally{//最终将要执行的一些代码
try {
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();
}
}
}
}
热门评论
加上注释,好理解多了!