JAVA的服务器和客户端怎么做

JAVA的服务器和客户端怎么做,第1张

楼上的太长了吧,我写个简单的
import javanet;
import javaio;
import javautil;
/
TCP协议下单向通信的客户端程序。
@author new

/
public class TCPClientA {
public static void main(String[] args) {
Socket s=null;
try {
s=new Socket("127001",8888);
BufferedReader br=new BufferedReader(new InputStreamReader(sgetInputStream()));
String str=brreadLine();
Systemoutprintln(str);
} catch (Exception e) {
eprintStackTrace();
}finally{
if(s!=null)try{sclose();}catch(IOException e){}
}
}
}
================================
import javanet;
import javaio;
import javautil;
/
TCP协议下单向通信的服务器端程序。
@author new

/
public class TCPServerA {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintStream ps=null;
try {
ss=new ServerSocket(8888);
while(true){
Systemoutprintln("服务器已启动,在8888端口:");
s=ssaccept();
Systemoutprintln("得到 "+sgetInetAddress()+" 的连接请求");
ps=new PrintStream(sgetOutputStream());
psprintln(new Date());
psflush();
Systemoutprintln("已向客户端发送数据!");
}
} catch (IOException e) {
eprintStackTrace();
}finally{
if(ps!=null)psclose();
if(s!=null)try{sclose();}catch(IOException e){}
if(ss!=null)try{ssclose();}catch(IOException e){}
}
}
}

你的不是B/S,B/S的意思是Browser(浏览器) Server(Web服务器),根据你的描述,你的客户端有3个类,服务端有2个类,是很明显的C/S模式,这个不需要建什么Web服务吧。
如果你想把你的程序运行起来,你首先要把服务端的代码执行起来,怎么执行?你把你服务端的两个类打成一个可执行的jar包(eclipse的export命令可以帮助你打可执行jar包),然后执行javaw -jar xxxxjar,把你的服务端程序跑起来。用同样的方式,把你的客户端程序跑起来就可以了。

服务器端:
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/12582628.html

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

发表评论

登录后才能评论

评论列表(0条)

保存