您的位置:程序门 -> java -> gui 设计



向高手求一程序代码


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


向高手求一程序代码
发表于:2008-01-10 16:33:46 楼主
本人现在急需一个java写的网络聊天程序,越简单越好.但最好是gui形式的.就像这样:

这个网址中的代码不能运行.由于我是初学java还请高手帮忙给个能运行的完整程序出来.谢谢.
发表于:2008-01-10 19:03:551楼 得分:0
java code
package test.cs; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; public class talkclient { public static void main(string[] args) { try { socket socket = new socket("211.144.155.20", 23); bufferedreader sin = new bufferedreader(new inputstreamreader(system.in)); printwriter os = new printwriter(socket.getoutputstream()); bufferedreader is = new bufferedreader(new inputstreamreader(socket.getinputstream())); string readline; readline = sin.readline(); while!readline.equals("bye")) { os.println(readline); os.flush(); system.out.println("server:" + is.readline()); readline = sin.readline(); } os.close(); is.close(); socket.close(); } catch (unknownhostexception e) { system.out.println("cann't find sever.program end."); system.exit(0); e.printstacktrace(); } catch (ioexception e) { system.out.println("can't find sever.program end"); system.exit(0); } catch (exception e) { e.getmessage(); } } }


java code
package test.cs; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; import java.util.arraylist; public class talkserver { public static void main(string[] args) { serversocket server = null; try { // boolean listening=true; try { server = new serversocket(14700); } catch (ioexception e) { system.out.println("can't listen to:" + e); e.getmessage(); } whiletrue) { socket socket = null; try { socket = server.accept(); // 接收客户连接 mythread workthread = new mythread(socket); // 创建一个工作线程 workthread.start(); // 启动工作线程 } catch (exception e) { system.out.println("error " + e); e.printstacktrace(); } } } catch (exception e) { system.out.println("error:" + e); e.getmessage(); } finally { try { if (server != null) server.close(); } catch (exception ex) { } } } } class mythread extends thread { private socket socket = null; public mythread(socket socket) { this.socket = socket; } public void run() { try { string line; bufferedreader is = new bufferedreader(new inputstreamreader(socket.getinputstream())); printwriter os = new printwriter(socket.getoutputstream()); line = is.readline(); while!line.equals("bye")) { os.println("client:" + line); os.flush(); line = is.readline(); object o=null; if( o instanceof string[]) {} } is.close(); os.close(); os.println("thread exit!"); } catch (exception ex) { ex.printstacktrace(); } finally { try { socket.close(); } catch (exception ex) { } } } public void listarray(arraylist list){ for(object o : list) { if(o instanceof string[]) { string[] strs = (string[])o; for(string str : strs) { system.out.println(str); } }else if(o instanceof arraylist) { listarray((arraylist)o); }else { system.out.println("not support type!!!"); continue; } } } }
发表于:2008-01-10 19:05:152楼 得分:0
发错了
java code
package test.cs; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; public class talkclient { public static void main(string[] args) { try { socket socket = new socket("localhost", 14700); // 用这个端口 bufferedreader sin = new bufferedreader(new inputstreamreader(system.in)); printwriter os = new printwriter(socket.getoutputstream()); bufferedreader is = new bufferedreader(new inputstreamreader(socket.getinputstream())); string readline; readline = sin.readline(); while!readline.equals("bye")) { os.println(readline); os.flush(); system.out.println("server:" + is.readline()); readline = sin.readline(); } os.close(); is.close(); socket.close(); } catch (unknownhostexception e) { system.out.println("cann't find sever.program end."); system.exit(0); e.printstacktrace(); } catch (ioexception e) { system.out.println("can't find sever.program end"); system.exit(0); } catch (exception e) { e.getmessage(); } } }
发表于:2008-01-11 12:26:463楼 得分:0
呵呵,刚才ping了一下"211.144.155.20",还真连上了。

你开着服务端,我们用你写的程序来聊聊啊
发表于:2008-01-14 16:01:154楼 得分:0
不错.谢了.不过我最近弄了一份gui的大家试试.
import   java.io.*;
import   java.awt.*;
import   java.net.*;
import   java.awt.event.*;
public   class   chatclient   extends   frame   implements   actionlistener
{
label   label=new   label("聊天");
        panel   panel=new   panel();
        textfield   tf=new   textfield(10);
        textarea   ta=new   textarea();
       
socket   client;
inputstream   in;
outputstream   out;

public   chatclient()
        {
  super("客户机");
setsize(250,250);
panel.add(label);
      panel.add(tf);
tf.addactionlistener(this);
add("north",panel);
add("center",ta);
addwindowlistener(new   windowadapter()
{public     void   windowclosing(windowevent   e)
{system.exit(0);}});
show();
 
try
{
//inetaddress.getlocalhost()
client=new   socket(192.168.1.112,5000);
ta.append("已连接到服务器:"+client.getinetaddress().gethostaddress()+"\n\n");
in=client.getinputstream();
out=client.getoutputstream();
}
catch(ioexception   ioe){}

while(true)
{
try
{
byte   []   buf=new   byte[256];
in.read(buf);
string   str=new   string(buf);
ta.append("服务器说:"+str);
ta.append("\n");
}
catch(ioexception   e){}
}
        }
public   void   actionperformed(actionevent   e)
{
try
{
string   str=tf.gettext();
byte[]   buf=str.getbytes();
tf.settext(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}
catch(ioexception   ioe){}
}
public   static   void   main   (string[]   args)   {
new   chatclient();
        }
}
发表于:2008-01-14 16:01:385楼 得分:0
import   java.io.*;
import   java.awt.*;
import   java.net.*;
import   java.awt.event.*;
public   class   chatserver   extends   frame   implements   actionlistener
{
label   label=new   label("聊天");
        panel   panel=new   panel();
        textfield   tf=new   textfield(10);
        textarea   ta=new   textarea();
       
serversocket   server;
socket   client;
inputstream   in;
outputstream   out;

public   chatserver()
        {
  super("服务器");
setsize(250,250);
panel.add(label);
      panel.add(tf);
tf.addactionlistener(this);
add("north",panel);
add("center",ta);
addwindowlistener(new   windowadapter()
{public     void   windowclosing(windowevent   e)
{system.exit(0);}});
show();
 
try
{
server=new   serversocket(5000);
client=server.accept();
ta.append("已连接的客户机:"+client.getinetaddress().gethostaddress()+"\n\n");
in=client.getinputstream();
out=client.getoutputstream();
}
catch(ioexception   ioe){}

while(true)
{
try
{
byte   []   buf=new   byte[256];
in.read(buf);
string   str=new   string(buf);
ta.append("客户机说:"+str);
ta.append("\n");
}
catch(ioexception   ioe){}
}
        }
public   void   actionperformed(actionevent   e)
{
try
{
string   str=tf.gettext();
byte[]   buf=str.getbytes();
tf.settext(null);
out.write(buf);
ta.append("我说:"+str);
ta.append("\n");
}
catch(ioexception   ioe){}
}
public   static   void   main   (string[]   args)   {
new   chatserver();
        }
}
发表于:2008-01-14 16:02:536楼 得分:0
虽然不太好看.但挺好用的.


快速检索

最新资讯
热门点击