java实现简单聊天软件
程序员文章站
2024-02-22 08:16:04
本文为大家分享了java实现简单聊天软件的具体代码,供大家参考,具体内容如下
介绍
这是我自己学习socket时写的,用到了多线程,socket的知识,还有java的i...
本文为大家分享了java实现简单聊天软件的具体代码,供大家参考,具体内容如下
介绍
这是我自己学习socket时写的,用到了多线程,socket的知识,还有java的io技术,并且做了一个java的ui,废话不多说,先上效果图
sendthread
这个类是用来实现数据的发送的,当按键按下时,会调用sendmessage方法
public class sendthread { chatwindow chatwindow; private string remoteip = ""; private int port = 0; private string message = ""; public sendthread(int port,chatwindow window) { chatwindow = window; } public void notrun() { inetsocketaddress isa = new inetsocketaddress(remoteip, port); try { socket socket = new socket(); socket.connect(isa); outputstreamwriter writer = new outputstreamwriter(socket.getoutputstream()); writer.write(message); writer.flush(); writer.close(); system.out.println("将数据写入到流中"); } catch (ioexception e) { e.printstacktrace(); }finally{ message = ""; } } public void senmessage(string host,int port,string message){ remoteip = host; this.port = port; this.message = message; notrun(); } }
receivethread
这个类实现的功能是维护一个阻塞线程,来对端口进行监听,接受收到的信息,每当收到信息时,都会显示。
public class receivethread extends thread { private serversocket server; private chatwindow window; public receivethread(chatwindow window) { this.window = window; try { server = new serversocket(0); window.setlocalport(server.getlocalport()); start(); } catch (ioexception e) { // todo auto-generated catch block window.printerror("连接出错"); } } @override public void run() { // todo auto-generated method stub while(true){ socket socket; try { socket = server.accept(); inputstreamreader reader = new inputstreamreader(socket.getinputstream()); int c; stringbuilder sb = new stringbuilder(); while((c = reader.read()) != -1){ sb.append((char)c); } window.setreceive(sb.tostring()); } catch (ioexception e) { // todo auto-generated catch block system.out.println("线程将接受到的数据写入对话框出错"); } } } }
chatwindow
这个类是ui的实现,通过绑定各个按钮的操作来实现别的功能
public class chatwindow extends jframe { private jbutton sendbutton; private jbutton cancelbutton; private jbutton receivebutton; private jtextarea receivetext; private jtextfield sendtext; private jlabel localport; private jtextfield remoteaddress; private jtextfield remoteport; private sendthread sendthread; private receivethread receivethread; private void guiini(){ container c = this.getcontentpane(); c.setlayout(new borderlayout()); setsize(400, 500); //第一个panel jpanel panel1 = new jpanel(new gridlayout(4, 2)); // jpanel panel1 = new jpanel(new (4, 2)); panel1.setsize(400, 75); panel1.add(new jlabel("当前机器的ip地址是:")); try { panel1.add(new jlabel(inetaddress.getlocalhost().gethostaddress())); } catch (unknownhostexception e) { // todo auto-generated catch block panel1.add(new jlabel("当前机器的ip地址是:" + "unknown")); } panel1.add(new jlabel("当前对话的端口是:")); panel1.add(localport = new jlabel(""+0)); panel1.add(new jlabel("remote host ip address:")); remoteaddress = new jtextfield(); remoteaddress.setcolumns(0); panel1.add(remoteaddress); panel1.add(new jlabel("remote host port:")); remoteport = new jtextfield(); remoteport.setcolumns(0); panel1.add(remoteport); c.add(panel1,borderlayout.north); //第二个panel jpanel panel2 = new jpanel(new flowlayout(flowlayout.leading)); panel2.setsize(400,200); panel2.add(new jlabel("收到的内容")); receivetext = new jtextarea(15,30); receivetext.seteditable(false); receivetext.setautoscrolls(true); jscrollpane jsp = new jscrollpane(receivetext); panel2.add(jsp); //第三个panel // jpanel panel3 = new jpanel(new gridlayout(2, 1)); panel2.add(new jlabel("请输入发送的内容")); sendtext = new jtextfield(30); sendtext.setautoscrolls(true); panel2.add(sendtext); c.add(panel2,borderlayout.center); //c.add(panel3); //第四个panel jpanel panel4 = new jpanel(new gridlayout(1, 0)); panel4.setsize(400,20); receivebutton = new jbutton("开始接受数据"); sendbutton = new jbutton("发送"); cancelbutton = new jbutton("取消"); panel4.add(receivebutton); panel4.add(cancelbutton); panel4.add(sendbutton); c.add(panel4,borderlayout.south); //四个面板内容设置完毕 setvisible(true); setresizable(false); setdefaultcloseoperation(exit_on_close); } private void actionini() { //键盘动作 this.addkeylistener(new keylistener() { @override public void keytyped(keyevent e) { } @override public void keyreleased(keyevent e) { // todo auto-generated method stub if(e.getkeycode() == keyevent.vk_enter){ //发送文本 sendthread.senmessage(remoteaddress.gettext(), integer.parseint(remoteport.gettext()), sendtext.gettext()); receivetext.settext(receivetext.gettext() + "\n" + "发送:" + sendtext.gettext()); } } @override public void keypressed(keyevent e) { // todo auto-generated method stub } }); //取消按钮的动作 cancelbutton.addmouselistener(new mouselistener() { @override public void mousereleased(mouseevent e) { // todo auto-generated method stub sendtext.settext(""); } @override public void mousepressed(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } }); sendbutton.addmouselistener(new mouselistener() { @override public void mousereleased(mouseevent e) { //发送文本 sendthread.senmessage(remoteaddress.gettext(), integer.parseint(remoteport.gettext()), sendtext.gettext()); receivetext.settext(receivetext.gettext() + "发送:" + sendtext.gettext()+ "\n"); } @override public void mousepressed(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } }); } private void threadini() { // todo auto-generated method stub sendthread = new sendthread(0, this); receivethread = new receivethread(this); } //构造函数 public chatwindow() { guiini(); actionini(); threadini(); } public void printerror(string err){ system.out.println("error occur:" + err); } //回调函数,用于接受从线程中返回的数据 public void setreceive(string receive){ receivetext.settext(receivetext.gettext() + "收到:" + receive+ "\n" ); } //当接受数据的线程开始工作以后,就调用该回调函数,设置当前聊天窗口使用的端口是哪个 public void setlocalport(int localporttext){ localport.settext(""+localporttext); } //放远,这个是整个程序的起点 public static void main(string[] args) { // todo auto-generated method stub new chatwindow(); } }
如何使用
将工程导入到eclipse中以后,运行两次,因为这个程序不属于单例模式,所以可以出现两个窗口,在远程端口,远程地址那个输入框中分别输入另一个窗口中显示的ip和端口号,接下来在输入框中输入要发送的文字就可以了
源码下载:java实现简单聊天软件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。