ITeye博客

java socket 处理多个客户端请求以及反馈信息

hufeng · 2019-01-23 11:43 · Java原创 · 编程语言 · Markdown

代码适用socket通讯 1.客户端请求2.服务器端解析请求3.服务器端反馈信息4.客户端接收信息5.客户端重复请求 客户端



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;

import com.transemic.acad.savaitem.SaveDrawingDialog;

public class SocketClient {
	public static void main(String[] args) {
		//客户端
		//1、创建客户端Socket,指定服务器地址和端口
		Socket socket;
		try {
			socket = new Socket("localhost",1024);
			//2、获取输出流,向服务器端发送信息
			OutputStream os = socket.getOutputStream();//字节输出流
			PrintWriter pw =new PrintWriter(os);//将输出流包装成打印流
			pw.write("用户名:admin;密码:123");
			pw.flush();
			socket.shutdownOutput();
			//3、获取输入流,并读取服务器端的响应信息
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String info = null;
			while((info=br.readLine())!=null){
				SaveDrawingDialog dialog = new SaveDrawingDialog();
				dialog.setTitle("显示服务器返回内容");

				dialog.setLabelContent(info);
				dialog.setModal(true);
				dialog.setVisible(true);
			}

			//4、关闭资源
			System.out.println("---关闭链接--");
			br.close();
			is.close();
			pw.close();
			os.close();
			socket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

服务器端



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;

import com.transemic.acad.savaitem.SaveDrawingDialog;

public class ServerSocketThread extends Thread {
	
	//服务器线程处理
	//和本线程相关的socket
	Socket socket = null;
	//
	public ServerSocketThread(Socket socket){
		this.socket = socket;
	}

	public void run(){
	//服务器处理代码
		//3、获取输入流,并读取客户端信息
		try {
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String info = null;
			while((info=br.readLine())!=null){
//			System.out.println("我是服务器,客户端说:"+info);
				SaveDrawingDialog dialog = new SaveDrawingDialog();
				dialog.setTitle("显示客户端请求内容");
				dialog.setLabelContent(info);
				dialog.setModal(true);
				dialog.setVisible(true);
			}
			socket.shutdownInput();//关闭输入流
			//4、获取输出流,响应客户端的请求
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
			pw.write("XXXXXX欢迎您!");
			pw.flush();


			//5、关闭资源
			pw.close();
			os.close();
			br.close();
			isr.close();
			is.close();
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		//============================================
		//服务器代码
		try {
			ServerSocket serverSocket = new ServerSocket(1024);
			Socket socket =null;
			int count =0;//记录客户端的数量
			while(true){
				socket = serverSocket.accept();
				ServerSocketThread serverThread = new ServerSocketThread(socket);
				serverThread.start();
				count++;
				System.out.println("客户端连接的数量:"+count);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


}

显示对话框



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class SaveDrawingDialog extends JDialog implements ActionListener{
	JLabel content;
	private String responseResult;
	public SaveDrawingDialog(){
		initPanel();
	}
	public String getResponseResult() {
		return responseResult;
	}
	public void setResponseResult(String responseResult) {
		this.responseResult = responseResult;
	}
	public void initPanel(){
		setTitle("保持图纸");
		setSize(300, 200);
		setAlwaysOnTop(true);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
		content = new JLabel();
		add(content, BorderLayout.CENTER);

		{
			JPanel buttonPane = new JPanel();
			add(buttonPane, BorderLayout.SOUTH);
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JButton okButton = new JButton("\u786E\u5B9A");
				okButton.addActionListener(this);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("\u53D6\u6D88");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);

				buttonPane.add(cancelButton);
			}
		}
	}
	public void setLabelContent(String conent){
		content.setText(conent);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("OK")){
			setVisible(false);
		}
		if(command.equals("Cancel")){
			setVisible(false);
		}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。