服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java网络通信技术之简单聊天小程序

java网络通信技术之简单聊天小程序

2021-05-20 13:43xlantian Java教程

这篇文章主要为大家详细介绍了java网络通信技术之简单聊天小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现简单聊天小程序的具体代码,供大家参考,具体内容如下

再学习完java的通信技术后,做了一个简单的窗体聊天程序。程序非常简单,主要目的是当练习巩固自己所学的东西,在这里写出来记录以下。下面直接上代码。

首先是服务端代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package chattwopackage;
 
import java.io.*;
import java.net.*;
 
public class chattwoserver {
 
 public chattwoserver(int port,string name) throws ioexception
 {
 serversocket server=new serversocket(port);//创建seversocket对象,提供tcp连接服务。指定端口port,等待tcp连接。
 system.out.print("正在等待连接,请勿操作!");
 socket client=server.accept();//创建socket对象,它等待接收客户端的连接。
 new chattwoclient(name,client);//实现图形界面。
 server.close();
 }
 
 public static void main(string[] args) throws ioexception {
 new chattwoserver(2001,"sq");
 
 }
 
}

然后是客户端的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package chattwopackage;
 
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
 
public class chattwoclient extends jframe implements actionlistener {
 private string name;
 private jtextarea text_re;
 private jtextfield text_se;
 private printwriter cout;
 private jbutton buttons[];
 public chattwoclient(string name,socket socket) throws ioexception
 {
 super("我:"+name+inetaddress.getlocalhost().gethostaddress()+":"+socket.getlocalport());
 this.setbounds(320, 240, 400, 240);
 this.text_re=new jtextarea();
 this.text_re.seteditable(false);
 this.getcontentpane().add(new jscrollpane(this.text_re));
 
 jtoolbar toolbar=new jtoolbar();
 this.getcontentpane().add(toolbar,"south");
 toolbar.add(this.text_se=new jtextfield(30));
 buttons=new jbutton[2];
 buttons[0]=new jbutton("发送");
 buttons[1]=new jbutton("下线");
 toolbar.add(buttons[0]);
 toolbar.add(buttons[1]);
 buttons[0].addactionlistener(this);
 buttons[1].addactionlistener(this);//给按钮添加事件监听,委托当前对象处理
 this.setvisible(true);
 this.name=name;
 this.cout=new printwriter(socket.getoutputstream(),true);//获得socket输出流
 this.cout.println(name);
 bufferedreader br=new bufferedreader(new inputstreamreader(socket.getinputstream())); //将socket的字节输入流转换为字符流,默认gbk字符集,再创建缓冲字符输入流
        string line="连接"+br.readline()+"成功";
        while(line!=null&&!line.endswith("bye"))
 {
  text_re.append(line+"\r\n");
  line=br.readline();
 }//读取对方发送的内容并显示,直到内容为为空或对方下线
 br.close();
 this.cout.close();
 socket.close();
 buttons[0].setenabled(false);
 buttons[1].setenabled(false);
 }
 public chattwoclient(string name,string host,int port) throws ioexception
 {
 this(name,new socket(host,port));//调用另一个构造方法
 }
 public void actionperformed(actionevent ev)
 {
 if(ev.getactioncommand().equals("发送"))
 {
  this.cout.println(name+":"+text_se.gettext());
  text_re.append("我:"+text_se.gettext()+"\n");
  text_se.settext("");
 }//按下发送按钮后,将内容发出,并更新自己聊天框的内容
 if(ev.getactioncommand().equals("下线"))
 {
  text_re.append("你已下线\n");
  this.cout.println(name+"离线\n"+"bye\n");
  buttons[0].setenabled(false);
  buttons[1].setenabled(false);
 }//下线按钮按下后,发送bye作为下线标记
 }
 
 
 public static void main(string[] args) throws ioexception {
 new chattwoclient("mxl","127.0.0.1",2001); //ip地址和端口
 
 }
 
}

运行效果:

java网络通信技术之简单聊天小程序

说明:

1.两台计算机一台作为服务端,作为服务端的计算机需要有两个代码。首先运行服务端的代码,等待客户端机器连接,客户端运行客户端代码后,提示连接成功。就可以发送信息了。

2.运行代码前需要将ip地址改为自己计算机当前的ip地址(modem、isdn、adsl、有线宽频、小区宽频等方式上网的计算机,每次上网所分配到的ip地址都不相同,这称为动态ip地址)。如果要用一台计算机充当客户端和服务端,就将ip地址写为:127.0.0.1(127.0.0.1是回送地址,指本地机,一般用来测试使用)。先运行服务端代码,再运行客户端代码即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xlantian/article/details/80441216

延伸 · 阅读

精彩推荐