linux多线程服务器发送结构体给客户端异常

linux多线程服务器发送结构体给客户端异常,第1张

服务器端:
import javaawt;
import javaawtevent;
import javaxswing;
import javaio;
import javanet;
import javautilVector;
public class OneToMoreServer extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
ServerSocket server = null;
Socket socket = null;BufferedReader instr =null;PrintWriter os=null ;
Vector vector=new Vector();
boolean serverRun=true;
boolean clientRun=true;
//Construct the frame
public OneToMoreServer() {
jbInit();

}

class MyThread extends Thread{//该线程负责接收数据
Socket socketI=null;
BufferedReader br=null;
public MyThread(Socket socket)
{
socketI=socket;
}
public void run(){
try{
while(clientRun){
thissleep(100);
br= new BufferedReader(new InputStreamReader(socketIgetInputStream()));
if(brready()){ //检查是否有数据
jTextArea1append("接收到来自客户端("+socketIgetInetAddress()toString()+")的消息: "+brreadLine()+"\n");
}
}
}catch(Exception ex){JOptionPaneshowMessageDialog(null,extoString());}
}
}

public void actionPerformed(ActionEvent e){
if(egetSource()==jButton1){
int port=IntegerparseInt(jTextField2getText()trim());
//监听指定端口
try
{
server = new ServerSocket(port);
new Thread(new ListenClient())start();
}
catch(IOException ex)
{
JOptionPaneshowMessageDialog(null,extoString());
}
}
if(egetSource()==jButton2){
String msg=jTextField3getText()trim();
if(msglength()!=0)
sendData("hello");
}
}
//该线程负责监听指定端口
class ListenClient implements Runnable
{
public void run()
{
try{
if(jButton1getText()trim()equals("侦听")){
jButton1setText("正在侦听");
while(serverRun)
{
Socket socketI=serveraccept();//有客户端连入时建立一个线程监听客户端发送的消息
vectoradd(socketI);
jButton1setText("正在聊天");
jTextArea1append("客户端"+socketIgetInetAddress()toString()+"已经连接到服务器\n");
MyThread t=new MyThread(socketI);
tstart();
}
}
}catch(Exception ex){
JOptionPaneshowMessageDialog(null,extoString());
}
}
}
private void sendData(String s){//发送数据
try{
for(int i=0;i<vectorsize();i++)
{
//怎么广播
//向每个客户端发送一条消息
Socket socket=(Socket)vectorget(i);
os= new PrintWriter(socketgetOutputStream());
osprintln(s);
osflush();
}
}catch(Exception ex){
}
}
private void jbInit() {
contentPane = (JPanel) thisgetContentPane();
contentPanesetLayout(null);
thissetSize(new Dimension(540, 340));
thissetTitle("服务器");
jLabel2setBounds(new Rectangle(22, 27, 72, 28));
jLabel2setText("端口号");
jLabel2setFont(new javaawtFont("宋体", 0, 14));
jTextField2setBounds(new Rectangle(113, 27, 315, 24));

jButton1setBounds(new Rectangle(440, 28, 73, 25));
jButton1setFont(new javaawtFont("Dialog", 0, 14));
jButton1setBorder(BorderFactorycreateEtchedBorder());
jButton1setActionCommand("jButton1");
jButton1setText("侦听");
jLabel3setBounds(new Rectangle(23, 57, 87, 28));
jLabel3setText("请输入信息");
jLabel3setFont(new javaawtFont("宋体", 0, 14));
jTextField3setBounds(new Rectangle(114, 60, 314, 24));
jTextField3setText("");
jButton2setText("广播");
jButton2setActionCommand("jButton1");
jButton2setBorder(BorderFactorycreateEtchedBorder());
jButton2setFont(new javaawtFont("Dialog", 0, 14));
jButton2setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1setBounds(new Rectangle(23, 92, 493, 189));

contentPaneadd(jTextField2, null);
contentPaneadd(jButton1, null);
contentPaneadd(jLabel3, null);
contentPaneadd(jTextField3, null);
contentPaneadd(jButton2, null);
contentPaneadd(jScrollPane1, null);
contentPaneadd(jLabel2, null);
jScrollPane1getViewport()add(jTextArea1, null);
jButton1addActionListener(this);
jButton2addActionListener(this);
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socketclose();
instrclose();
Systemexit(0);
}catch(Exception ex){
//JOptionPaneshowMessageDialog(null,extoString());
}

}
});
}
public static void main(String arg[]){
JFramesetDefaultLookAndFeelDecorated(true);
OneToMoreServer frm=new OneToMoreServer();
frmsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
frmsetVisible(true);
}
}
客户端
import javaawt;
import javaawtevent;
import javaxswing;
import javaio;
import javanet;
public class Client extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField("127001");
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
BufferedReader instr =null;
Socket socket = null;
PrintWriter os=null;
public Client() {
jbInit();
}
class MyThread extends Thread{
public void run(){
try{
os=new PrintWriter(socketgetOutputStream());
instr=new BufferedReader(new InputStreamReader(socketgetInputStream()));
while(true)
{
thissleep(100);
if(instrready())
{
jTextArea1append("接收到来自服务器的消息: "+instrreadLine()+"\n");
}
}
}catch(Exception ex){
JOptionPaneshowMessageDialog(null,extoString());
}
}
}
public void actionPerformed(ActionEvent e){
if(egetSource()==jButton1){
String ip=jTextField3getText()trim();
int port=IntegerparseInt(jTextField2getText()trim());
connectServer(ip,port);
}
if(egetSource()==jButton2){
String s=thisjTextField3getText()trim();
sendData(s);
}
}
private void connectServer(String ip,int port){//连接
try{
if(jButton1getText()trim()equals("连接")){
jButton1setText("连接服务器");
socket=new Socket(ip,port);
jButton1setText("正在聊天");
MyThread t=new MyThread();
tstart();
}
}catch(Exception ex){
JOptionPaneshowMessageDialog(this,extoString());
}
}
private void sendData(String s){//发送数据
try{
os = new PrintWriter(socketgetOutputStream());
osprintln(s);
osflush();
thisjTextArea1append("向服务器发送消息:"+s+"\n");
}catch(Exception ex){
JOptionPaneshowMessageDialog(this,extoString());
}
}
private void jbInit() {
contentPane = (JPanel) thisgetContentPane();
jLabel1setFont(new javaawtFont("宋体", 0, 14));
jLabel1setText("服务器名称");
jLabel1setBounds(new Rectangle(20, 22, 87, 28));
contentPanesetLayout(null);
thissetSize(new Dimension(540, 340));
thissetTitle("客户端");

jTextField1setBounds(new Rectangle(114, 26, 108, 24));
jLabel2setBounds(new Rectangle(250, 25, 72, 28));
jLabel2setText("端口号");
jLabel2setFont(new javaawtFont("宋体", 0, 14));
jTextField2setBounds(new Rectangle(320, 27, 108, 24));

jButton1setBounds(new Rectangle(440, 28, 73, 25));
jButton1setFont(new javaawtFont("Dialog", 0, 14));
jButton1setBorder(BorderFactorycreateEtchedBorder());
jButton1setActionCommand("jButton1");
jButton1setText("连接");
jLabel3setBounds(new Rectangle(23, 57, 87, 28));
jLabel3setText("请输入信息");
jLabel3setFont(new javaawtFont("宋体", 0, 14));
jTextField3setBounds(new Rectangle(114, 60, 314, 24));

jButton2setText("发送");
jButton2setActionCommand("jButton1");
jButton2setBorder(BorderFactorycreateEtchedBorder());
jButton2setFont(new javaawtFont("Dialog", 0, 14));
jButton2setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1setBounds(new Rectangle(23, 92, 493, 189));

contentPaneadd(jLabel1, null);
contentPaneadd(jTextField1, null);
contentPaneadd(jLabel2, null);
contentPaneadd(jTextField2, null);
contentPaneadd(jButton1, null);
contentPaneadd(jLabel3, null);
contentPaneadd(jTextField3, null);
contentPaneadd(jButton2, null);
contentPaneadd(jScrollPane1, null);
jScrollPane1getViewport()add(jTextArea1, null);
jButton1addActionListener(this);
jButton2addActionListener(this);
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socketclose();instrclose();osclose();Systemexit(0);
}catch(Exception ex){
JOptionPaneshowMessageDialog(null,extoString());
}
}
});
}
public static void main(String arg[]){
JFramesetDefaultLookAndFeelDecorated(true);
Client frm=new Client();
frmsetVisible(true);
}
}

是的。
很多服务器端程序都是允许被多个应用程序访问的,例如门户网站可以被多个用户同时访问,因此服务器都是多线程的。
主线程用于监听套接字,有新的连接建立之后创建一个副线程,主线程继续监听。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/12781026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存