Java 聊天小程序
大家帮忙看一下啊。基本要求:程序界面包括消息记录区,消息编辑区,发送按钮,当前在线用户列表,支持私聊。提示:使用java.net包下的SeerverSocket/Sock...
大家帮忙看一下啊。基本要求:程序界面包括消息记录区,消息编辑区,发送按钮,当前在线用户列表,支持私聊。提示:使用java.net包下的SeerverSocket/Socket以及java.io包下的类!如果能给出完整代码感激万分……
展开
3个回答
展开全部
不知道你为什么要用这个
流
datainputstream
sin
=
new
datainputstream(system.in);
你程序没反应是你读的时候一直阻塞着。你用
bufferedreader试试
肯定有反应。
流
datainputstream
sin
=
new
datainputstream(system.in);
你程序没反应是你读的时候一直阻塞着。你用
bufferedreader试试
肯定有反应。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
至于作业的话
还是找一个比较专业的
论坛
看看csdn
应该
不错
还是找一个比较专业的
论坛
看看csdn
应该
不错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
客户端:
package clientsocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;
public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();
}
public void connect(){
try{s=new Socket("127.0.0.1",8888);//127.0.0.1是你自己的ip号
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}
}
public void createUI(){
Frame f=new Frame("Client");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyClientListener listener=new MyClientListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);
}
public void createThread(){
MyClientReader reader=new MyClientReader(this);
reader.start();
}
public void close(){
try{
dis.close();
dos.close();
s.close();
}
catch(IOException e){ e.printStackTrace();}
}
public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}
class MyClientListener implements ActionListener{
private Main client;
public MyClientListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();
}
}
class MyClientReader extends Thread {
private Main client;
public MyClientReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
服务器端:
package serversocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;
public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();
}
public void connect(){
try{ss=new ServerSocket(8888); s=ss.accept();
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}
}
public void createUI(){
Frame f=new Frame("Server");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyServerListener listener=new MyServerListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);
}
public void createThread(){
MyServerReader reader=new MyServerReader(this);
reader.start();
}
public void close(){
try{
dis.close();
dos.close();
s.close();
ss.close();
}
catch(IOException e){ e.printStackTrace();}
}
public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}
class MyServerListener implements ActionListener{
private Main client;
public MyServerListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();
}
}
class MyServerReader extends Thread {
private Main client;
public MyServerReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
要先运行服务器端,再运行客户端!
package clientsocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;
public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();
}
public void connect(){
try{s=new Socket("127.0.0.1",8888);//127.0.0.1是你自己的ip号
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}
}
public void createUI(){
Frame f=new Frame("Client");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyClientListener listener=new MyClientListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);
}
public void createThread(){
MyClientReader reader=new MyClientReader(this);
reader.start();
}
public void close(){
try{
dis.close();
dos.close();
s.close();
}
catch(IOException e){ e.printStackTrace();}
}
public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}
class MyClientListener implements ActionListener{
private Main client;
public MyClientListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();
}
}
class MyClientReader extends Thread {
private Main client;
public MyClientReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
服务器端:
package serversocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;
public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();
}
public void connect(){
try{ss=new ServerSocket(8888); s=ss.accept();
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}
}
public void createUI(){
Frame f=new Frame("Server");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyServerListener listener=new MyServerListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);
}
public void createThread(){
MyServerReader reader=new MyServerReader(this);
reader.start();
}
public void close(){
try{
dis.close();
dos.close();
s.close();
ss.close();
}
catch(IOException e){ e.printStackTrace();}
}
public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}
class MyServerListener implements ActionListener{
private Main client;
public MyServerListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();
}
}
class MyServerReader extends Thread {
private Main client;
public MyServerReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
要先运行服务器端,再运行客户端!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询