备注:必须先运行服务器端,然后运行客户端
1.客户端
LoginClient.java
package pb.socket.LoginStr;
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 LoginClient {
public static void main(String[] args) {
try {
//1.建立客户端socket连接,指定服务器位置及端口
Socket socket =new Socket("localhost",8800);
//2.得到socket读写流
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
//输入流
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//3.利用流按照一定的操作,对socket进行读写操作
String info="用户名:Tom,用户密码:123456";
pw.write(info);
pw.flush();
socket.shutdownOutput();
//接收服务器的相应
String reply=null;
while(!((reply=br.readLine())==null)){
System.out.println("接收服务器的信息:"+reply);
}
//4.关闭资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.服务器端
LoginServer.java
package pb.socket.LoginStr;
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.ServerSocket;
import java.net.Socket;
public class LoginServer {
public static void main(String[] args) {
try {
//1.建立一个服务器Socket(ServerSocket)绑定指定端口
ServerSocket serverSocket=new ServerSocket(8800);
//2.使用accept()方法阻止等待监听,获得新连接
Socket socket=serverSocket.accept();
//3.获得输入流
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//获得输出流
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
//4.读取用户输入信息
String info=null;
while(!((info=br.readLine())==null)){
System.out.println("我是服务器,用户信息为:"+info);
}
//给客户一个响应
String reply="welcome";
pw.write(reply);
pw.flush();
//5.关闭资源
pw.close();
os.close();
br.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}