package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketService {
//搭建服务器端
public static void main(String[] args) throws IOException{
SocketService socketService = new SocketService();
//1、a)创建一个服务器端Socket,即SocketService
socketService.oneServer();
}
public void oneServer(){
try{
ServerSocket server=null;
try{
server=new ServerSocket(5209);
//b)指定绑定的端口,并监听此端口。
System.out.println("服务器启动成功");
//创建一个ServerSocket在端口5209监听客户请求
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
//出错,打印出错信息
}
Socket socket=null;
try{
socket=server.accept();
//2、调用accept()方法开始监听,等待客户端的连接
//使用accept()阻塞等待客户请求,有客户
//请求到来则产生一个Socket对象,并继续执行
}catch(Exception e) {
System.out.println("Error."+e);
//出错,打印出错信息
}
//3、获取输入流,并读取客户端信息
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter writer=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
System.out.println("Client:"+in.readLine());
//在标准输出上打印从客户端读入的字符串
line=br.readLine();
//从标准输入读入一字符串
//4、获取输出流,响应客户端的请求
while(!line.equals("end")){
//如果该字符串为 "bye",则停止循环
writer.println(line);
//向客户端输出该字符串
writer.flush();
//刷新输出流,使Client马上收到该字符串
System.out.println("Server:"+line);
//在系统标准输出上打印读入的字符串
System.out.println("Client:"+in.readLine());
//从Client读入一字符串,并打印到标准输出上
line=br.readLine();
//从系统标准输入读入一字符串
} //继续循环
//5、关闭资源
writer.close(); //关闭Socket输出流
in.close(); //关闭Socket输入流
socket.close(); //关闭Socket
server.close(); //关闭ServerSocket
}catch(Exception e) {//出错,打印出错信息
System.out.println("Error."+e);
}
}
}
服务器端代码:
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public void server(){
try {
ServerSocket serverSocket = new ServerSocket(8888); //建立服务器端的套接字,并监听8888端口
Socket socket = serverSocket.accept(); //如果有请求到达,则接受请求,并建立一个新的套接字
InputStream is = socket.getInputStream(); //获取此套接字上的输入流,用于接收消息
//接下来就是获取套接字里的字节流,并转化为字符串
byte[] bytes = new byte[1024];
int count;
StringBuilder sb = new StringBuilder();
while((count = is.read(bytes)) != -1){
sb.append(new String(bytes, 0, count));
}
is.close();
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server s = new Server();
s.server();
}
}
另附客户端:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
public void client(){
try {
Socket socket = new Socket(); //建立套接字
socket.connect(new InetSocketAddress("127.0.0.1", 8888)); //连接到指定IP地址的主机,并指定端口号接收消息
OutputStream os = socket.getOutputStream(); //获取此连接上的输出流,用于发送数据
os.write(new String("Hello, world!").getBytes()); //将字符串转化为字节数组写入输出流
os.flush();//刷新此输出流,将字节流写入它们预期的目标
os.close(); //关闭连接,否则会报连接重置异常
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client c = new Client();
c.client();
}
}