永发信息网

JAVA简单的socket程序

答案:5  悬赏:80  手机版
解决时间 2021-05-09 04:16
  • 提问者网友:暮烟疏雨之际
  • 2021-05-08 19:39
能不能写一个简单的socket程序,有三个小程序,其中一个是服务器。就是其中一个客服端发一个信息给服务器,然后服务器作出一个回应,将这个回应发送到两个客服端上,这个程序能行么。在一台机子上哈。谢谢。
最佳答案
  • 五星知识达人网友:逐風
  • 2021-05-08 21:01

CS001店面是开始发送信息的客户端~


这个是我要参加比赛预测的题目,然后稍微改下就符合你的要求了~


服务器端:



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;


import javax.swing.JOptionPane;



public class GUI与多线程 extends Frame implements ActionListener,ItemListener
{
Panel p1,p2,p3;
Checkbox c1,c2;
CheckboxGroup group;
TextArea text1 = null,text2 = null;
TextField tf = null;
Button send,exit;
CS001Thread th1 = null;
CS002Thread th2 = null;
int flag = 0;
public GUI与多线程(String title)
{
super(title);
p1 = new Panel();
group = new CheckboxGroup();
c1 = new Checkbox("CS001",group,false);
c2 = new Checkbox("CS002",group,false);
p1.add(c1);
p1.add(c2);
p1.add(new Label("销售信息",Label.RIGHT));
p2 = new Panel();
text1 = new TextArea(30,25);
text1.setEditable(false);
text2 = new TextArea(30,25);
text2.setEditable(false);
p2.add(text1);
p2.add(text2);

p3 = new Panel();
tf = new TextField(20);
send = new Button("发送");
exit = new Button("退出");
p3.add(new Label("输入信息"));
p3.add(tf);
p3.add(send);
p3.add(exit);

add(p1,"North");
add(p2);
add(p3,"South");

setSize(480, 300);
setVisible(true);

send.addActionListener(this);
exit.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);

th1 = new CS001Thread(text1);
th1.start();


th2 = new CS002Thread(text2);
th2.start();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public static void main(String[] args)
{
new GUI与多线程("WF总公司办公室");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exit)
System.exit(0);
else
if(flag == 1)
{
try
{
th1.out.writeUTF("WF总公司办公室:" + tf.getText() + "\n");
text1.append("WF总公司办公室:" + tf.getText() + "\n");
tf.setText("");
}
catch (Exception e1)
{
text1.append("数据流通道没有建立\n");
}
}
else
if(flag == 2)
{
try
{
th2.out.writeUTF("WF总公司办公室:" + tf.getText() + "\n");
text2.append("WF总公司办公室:" + tf.getText() + "\n");
tf.setText("");
}
catch (IOException e1)
{
text2.append("数据流通道没有建立\n");
}
}
else
{
tf.setText("");
JOptionPane.showMessageDialog(this, "请选择CS001/CS002店面!");
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable() == c1)
{
flag = 1;

}
else
flag = 2;
}
}
class CS001Thread extends Thread
{
ServerSocket server;
Socket socket;
TextArea text1;


DataOutputStream out;
DataInputStream in;
public CS001Thread(TextArea text1)
{
this.text1 = text1;
}


public void run()
{
try
{
server = new ServerSocket(2230);
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());

talk();
}
catch (IOException e)
{
text1.append("没有建立通道");
}
}
public void talk()
{

while(true)
{
try
{
text1.append(in.readUTF());
out.writeUTF("WF总公司办公室:哦··!\n");
CS002Thread.out.writeUTF("WF总公司办公室:哦··!\n");
}
catch (IOException e)
{
text1.append("数据流通道没有建立\n");

}
}
}
}
class CS002Thread extends Thread
{
ServerSocket server;
Socket socket;
TextArea text2;

static DataOutputStream out;
DataInputStream in;
public CS002Thread(TextArea text2)
{
this.text2 = text2;

}

public void run()
{
try
{
server = new ServerSocket(5670);
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());

talk();
}
catch (IOException e)
{
text2.append("没有建立通道");
}
}
public void talk()
{

while(true)
{
try
{
text2.append(in.readUTF());

}
catch (IOException e)
{
text2.append("数据流通道没有建立\n");

}
}
}
}


客户端1:



import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class GUI与多线程2 extends Frame implements ActionListener
{
TextArea text;
Panel p;
TextField tf;
Button send,exit;
Panel p2;

Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
public GUI与多线程2(String title)
{
super(title);


p2 = new Panel();
text = new TextArea(18,60);
text.setEditable(false);
p2.add(text);


p = new Panel();
tf = new TextField(10);
send = new Button("发送");
exit = new Button("退出");
p.add(new Label("输入 信息"));
p.add(tf);
p.add(send);
p.add(exit);


add(new Label("收到信息",Label.CENTER),"North");
add(p2);
add(p,"South");


setSize(480, 400);
setVisible(true);


send.addActionListener(this);
exit.addActionListener(this);


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
try
{
socket = new Socket(InetAddress.getLocalHost(),2230);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());

}
catch (Exception e1)
{
e1.printStackTrace();
}


}


public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exit)
System.exit(0);
else
{
try
{
out.writeUTF("CS001店办公室:" +tf.getText() + "\n");
text.append("CS001店办公室:" +tf.getText() + "\n");
tf.setText("");
}
catch (IOException ex)
{
text.append("数据流通道没有建立\n");
}
}
}
public void talk()
{
while(true)
{
try
{
text.append(in.readUTF());
}
catch (IOException e)
{
text.append("数据流通道没有建立\n");
}
}
}
public static void main(String[] args)
{
new GUI与多线程2("CS001店办公室").talk();
}
}


package Test;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;


public class GUI与多线程3 extends Frame implements ActionListener
{
TextArea text;
Panel p;
TextField tf;
Button send,exit;
Panel p2;

Socket socket;
DataOutputStream out;
DataInputStream in;
public GUI与多线程3(String title)
{
super(title);


p2 = new Panel();
text = new TextArea(18,60);
text.setEditable(false);
p2.add(text);


p = new Panel();
tf = new TextField(10);
send = new Button("发送");
exit = new Button("退出");
p.add(new Label("输入 信息"));
p.add(tf);
p.add(send);
p.add(exit);


add(new Label("收到信息",Label.CENTER),"North");
add(p2);
add(p,"South");


setSize(480, 400);
setVisible(true);


send.addActionListener(this);
exit.addActionListener(this);


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
try
{
socket = new Socket(InetAddress.getLocalHost(),5670);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
}
catch (Exception e1)
{
e1.printStackTrace();
}


}


public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exit)
System.exit(0);
else
{
try
{
out.writeUTF("CS002店办公室:" + tf.getText() + "\n");
text.append("CS002店办公室:" + tf.getText() + "\n");
tf.setText("");
}
catch (IOException ex)
{
text.append("数据流通道没有建立\n");
}
}
}
public void talk()
{
while(true)
{
try
{
text.append(in.readUTF());
}
catch (IOException e)
{
text.append("数据流通道没有建立\n");
}
}
}
public static void main(String[] args)
{
new GUI与多线程3("CS002店办公室").talk();
}
}

全部回答
  • 1楼网友:洎扰庸人
  • 2021-05-09 00:25

是可以的,客户端可以多次运行,前提是服务器端必需采用多线程,或是非阻塞的方式来监听客户端的连接请求。

服务器端 思想,当客户端连接到来时,利用这个socket创建一个新的线程,并启动。当然,这只不过是最简单的性能最差的方法。你可以使用线程池在java.util.concurrent包中有线程池的实现类Executors你可以通过它的ExecutorService executorService=Executors.newFixedThreadPool(int threadSize);来创建一个线程池。当客户端连接到来时你可以创建一个线程类(handler)然后调用线程池的execute方法指行一个此线程。如:executorService.execute(new Handler(socket));

。当然,如果你考虑性能,那么你最好使用nio(非阻塞)来处理,在java.nio.channels包下面有相应的类能处理nio。你可以去查看相关的资料。。

  • 2楼网友:掌灯师
  • 2021-05-08 23:23
你可以通过线程来让服务器接受多个客户端!只需要一个端口就就可以,不明白你为什么需要这么多端口!下面是我写的一个实现接受多个客户端的程序,你参考一下吧!import java.io.*;import java.net.*;public class muitichatserver {private static final int serverport=2019;public static void main(String[]args){ System.out.println("一个简陋的聊天程序嘿嘿"); try{ ServerSocket server=new ServerSocket(serverport); System.out.println("开始监听喽~"); Socket client=server.accept(); System.out.println("有连入!"); new chatThread(client).start(); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(client.getOutputStream()); while(true){ String keyin=in.readLine(); System.out.println(keyin); out.println(keyin); out.flush(); } }catch(Exception e) {System.out.println("NND 有错呀!"); }}} //import java.io.BufferedReader;import java.io.InputStreamReader; import java.net.Socket; class chatThread extends Thread{ private Socket client; public chatThread(Socket c) { client=c; } public void run(){ try{ BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); while(true){ String s=in.readLine(); if(s!=null) System.out.println(s); } //in.close(); } catch(Exception e) {System.out.println("线程chatthread出错!");} }}
  • 3楼网友:爱难随人意
  • 2021-05-08 22:36

服务端采用多线程来写,客户端的接受也要用多线程,因为我现在没时间,如果以后有时间的话再来写写

  • 4楼网友:舊物识亽
  • 2021-05-08 21:47
可以!套接字来传输!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯