概述
服务器 ---- 客户端 (简单通讯实例)
(使用方法先启动服务器,再启动客户端。先用客户端给服务器发送 内容)
器
服务器
//----------------------------------------------------------------------------------------
import java .net.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//做一个服务器,与客户端做交流
//使用规则:必须先打开一个服务器,然后才能与客户端做交流,必须是客户端先发送内容
public class khd extends JFrame implements ActionListener
{
JTextArea wby;
String ip;
JPanel mb,mb1;
JComboBox xlk;
JTextArea lt;
JButton fs,qk;
JScrollPane gd,gd1;
PrintWriter pw ;
JTextField x1;
public khd ()
{
wby = new JTextArea();
wby.setBackground(Color.PINK);
//将文本域添加到滚动条中
gd = new JScrollPane(wby);
String [] xx = {"IP","端口"};
//将字符串数组里的内容添加到,下拉框列表中
xlk = new JComboBox(xx);
xlk.addActionListener(this);
fs = new JButton("发送");
fs.setBackground(Color.green);//设置按钮 的颜色
qk = new JButton("清空聊天记录");
qk.addActionListener(this);
qk.setBackground(Color.gray);
x1 = new JTextField(10);
lt = new JTextArea();
gd1 = new JScrollPane(lt);
mb = new JPanel();
mb.setLayout(new FlowLayout());
mb.add(xlk);
mb.add(x1);
mb.add(fs);
mb.add(qk);
mb1 = new JPanel();
mb1.setLayout(new BorderLayout());
mb1.add("North",mb);
mb1.add(gd1);
fs.addActionListener(this);
this.setLayout(new GridLayout(2,1));
this.add(gd);
this.add(mb1);
//键盘监听
Keyevent();
this.setTitle("服务器");
this.getContentPane().setBackground(new Color(90,45,00));//设置窗口颜色其中100 101 102分别为R G B的值
//范围在0-255 自己怎么设都行
//设置窗口的头像(唐尼)
this.setIconImage(new ImageIcon("aa/2.jpg").getImage());
this.setSize(450,400);
this.setLocation(400, 200);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
//获取本地IP链接
ip = InetAddress.getLocalHost().getHostAddress();
ServerSocket ss = new ServerSocket(9999);
System.out.println("服务器正在监听");
Socket s = ss.accept();//用来接受客户端的反应
//用来接收客户端的内容
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(),true);
// InputStreamReader js = new InputStreamReader(System.in);//接收控制台的内容用于服务器端的发送
// BufferedReader br1 = new BufferedReader(js);
while(true)
{
String jieshou = br.readLine();//用来接受客户端的内容
//不断的接受用户的发来的内容 ,不断在文本域中显示
wby.append("客户端:"+jieshou+"n");
}
}
catch(Exception e){}
}
public static void main(String[] args) {
new khd();
}
//发送内容
public void fasongnr()
{
//获取需要发送的内容
String fasong = lt.getText();
if(fasong.equals("")){
JOptionPane.showMessageDialog(this,"消息为空,发送失败!");
}
else{
wby.append("我发言(服务器):" +fasong+ "rn");
pw.println(fasong);
}
lt.setText("");//清空聊天窗口的内容
}
//键盘监听
public void Keyevent()
{
lt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
fasongnr();
}
});
}
//按钮监听
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == fs)
{
fasongnr();
}
if(e.getSource() == qk)
{
wby.setText("");
}
if(xlk.getSelectedItem().equals("IP"))
{
x1.setText(ip);
}
if(xlk.getSelectedItem().equals("端口"))
{
x1.setText("9999");
}
}
}
//注意在编写服务器,与客户端之间的通信区别的时候
// 服务器 需要设置端口:ServerSocket ss = new ServerSocket(9999); 和接受客户端的反应 :Socket s = ss.accept();
//而 客户端 需要设置:来自所需客户端的ip和端口,Socket ss = new Socket ("127.0.0.1",9999);
客户端
---------------------------------------------------------------------------------------------------------
import java.net.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class khdfz1 extends JFrame implements ActionListener {
String ip;
JTextArea wby;
JPanel mb, mb1;
JComboBox xlk;
JTextArea lt;
JButton fs, qk;
JScrollPane gd, gd1;
PrintWriter pw;
JTextField x1;
public khdfz1() {
wby = new JTextArea();
wby.setBackground(Color.ORANGE);
gd = new JScrollPane(wby);
String[] xx = { "IP", "端口" };
xlk = new JComboBox(xx);
xlk.addActionListener(this);
fs = new JButton("发送");
fs.setBackground(Color.green);// 设置按钮 的颜色
qk = new JButton("清空聊天记录");
qk.addActionListener(this);
qk.setBackground(Color.gray);
x1 = new JTextField(10);
lt = new JTextArea();
gd1 = new JScrollPane(lt);
mb = new JPanel();
mb.setLayout(new FlowLayout());
mb.add(xlk);
mb.add(x1);
mb.add(fs);
mb.add(qk);
mb1 = new JPanel();
mb1.setLayout(new BorderLayout());
mb1.add("North", mb);
mb1.add(gd1);
fs.addActionListener(this);
this.setLayout(new GridLayout(2, 1));
this.add(gd);
this.add(mb1);
this.setTitle("客户端1");
this.getContentPane().setBackground(new Color(90, 45, 00));// 设置窗口颜色其中100
// 101
// 102分别为R G
//键盘监听
Keyevent(); // B的值
// 范围在0-255 自己怎么设都行
this.setIconImage(new ImageIcon("aa/2.jpg").getImage());
this.setSize(450, 400);
this.setLocation(400, 200);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
// 获取本地IP链接
ip = InetAddress.getLocalHost().getHostAddress();
Socket s = new Socket(ip, 9999);// 客户端接受服务器的IP
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr); // 用来接受服务器端的内容
pw = new PrintWriter(s.getOutputStream(), true);
while (true) {
String jieshou = br.readLine();// 用来接受服务器端的内容
wby.append("服务器:" + jieshou + "n");// 直接用于文本域的内容
}
} catch (Exception e) {
}
}
public static void main(String[] args) {
new khdfz1();
}
//发送内容
public void fasongnr()
{
//获取需要发送的内容
String fasong = lt.getText();
if(fasong.equals("")){
JOptionPane.showMessageDialog(this,"消息为空,发送失败!");
}
else{
wby.append("我发言(服务器):" +fasong+ "rn");
pw.println(fasong);
}
lt.setText("");//清空聊天窗口的内容
}
//键盘监听
public void Keyevent()
{
lt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
fasongnr();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == fs) {
fasongnr();
}
if (e.getSource() == qk) {
wby.setText("");
}
if(xlk.getSelectedItem().equals("IP"))
{
x1.setText(ip);
}
if(xlk.getSelectedItem().equals("端口"))
{
x1.setText("9999");
}
}
}
最后
以上就是洁净猫咪为你收集整理的服务器--客户端通讯的全部内容,希望文章能够帮你解决服务器--客户端通讯所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复