求一个简单的 局域网聊天工具 的 JAVA代码。 只要可以发送文字就行。 谢谢!!! 10
3个回答
展开全部
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ServerChat extends JFrame implements Runnable{
/**
* @param args
*/
JLabel lblSend=new JLabel("请输入要发送的信息");
JTextField txtSend=new JTextField(20);
JTextArea txaContent=new JTextArea(10,20);
JButton btnSend=new JButton("发送");
Socket s=null;
ServerSocket sc=null;
boolean flag=true;
public ServerChat(){
super("服务器端");
init();
}
private void init(){
setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lblSend);p1.add(txtSend);p1.add(btnSend);
add(p1,BorderLayout.NORTH);
add(txaContent,BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
String tmp=txtSend.getText().trim();
txaContent.append("我说:"+tmp+"\n");
dos.writeUTF(tmp);
txtSend.setText("");
if(tmp.trim().equals("bye"))
{
flag=false;s.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
try {
sc=new ServerSocket(3000);
for(int i=0;i<2;i++)
s=sc.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setSize(400,400);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
while (flag) {
String tmp = dis.readUTF();
txaContent.append(tmp+"\n");
if(tmp.trim().equals("bye"))
{
s.close();break;
}
}
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerChat c=new ServerChat();
new Thread(c).start();
}
}
package ch4;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientChat extends JFrame implements Runnable {
/**
* @param args
*/
JLabel lblSend = new JLabel("请输入要发送的信息");
JTextField txtSend = new JTextField(20);
JTextArea txaContent = new JTextArea(10, 20);
JButton btnSend = new JButton("发送");
Socket s = null;
boolean flag=true;
public ClientChat() {
super("客户端");
init();
}
private void init() {
setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lblSend);
p1.add(txtSend);
p1.add(btnSend);
add(p1, BorderLayout.NORTH);
add(txaContent, BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s
.getInputStream());
String tmp = txtSend.getText().trim();
txaContent.append("我说:" + tmp+"\n");
txtSend.setText("");
if(tmp.trim().equals("bye")){
s.close();flag=false;
}
try {
dos.writeUTF(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
try {
s = new Socket("localhost", 3000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setSize(400, 400);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
while (true) {
String tmp = dis.readUTF();
txaContent.append(tmp+"\n");
if(tmp.trim().equals("bye"))
{
s.close();break;
}
}
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientChat c = new ClientChat();
new Thread(c).start();
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ServerChat extends JFrame implements Runnable{
/**
* @param args
*/
JLabel lblSend=new JLabel("请输入要发送的信息");
JTextField txtSend=new JTextField(20);
JTextArea txaContent=new JTextArea(10,20);
JButton btnSend=new JButton("发送");
Socket s=null;
ServerSocket sc=null;
boolean flag=true;
public ServerChat(){
super("服务器端");
init();
}
private void init(){
setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lblSend);p1.add(txtSend);p1.add(btnSend);
add(p1,BorderLayout.NORTH);
add(txaContent,BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try {
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
String tmp=txtSend.getText().trim();
txaContent.append("我说:"+tmp+"\n");
dos.writeUTF(tmp);
txtSend.setText("");
if(tmp.trim().equals("bye"))
{
flag=false;s.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
try {
sc=new ServerSocket(3000);
for(int i=0;i<2;i++)
s=sc.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setSize(400,400);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
while (flag) {
String tmp = dis.readUTF();
txaContent.append(tmp+"\n");
if(tmp.trim().equals("bye"))
{
s.close();break;
}
}
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerChat c=new ServerChat();
new Thread(c).start();
}
}
package ch4;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientChat extends JFrame implements Runnable {
/**
* @param args
*/
JLabel lblSend = new JLabel("请输入要发送的信息");
JTextField txtSend = new JTextField(20);
JTextArea txaContent = new JTextArea(10, 20);
JButton btnSend = new JButton("发送");
Socket s = null;
boolean flag=true;
public ClientChat() {
super("客户端");
init();
}
private void init() {
setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lblSend);
p1.add(txtSend);
p1.add(btnSend);
add(p1, BorderLayout.NORTH);
add(txaContent, BorderLayout.CENTER);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s
.getInputStream());
String tmp = txtSend.getText().trim();
txaContent.append("我说:" + tmp+"\n");
txtSend.setText("");
if(tmp.trim().equals("bye")){
s.close();flag=false;
}
try {
dos.writeUTF(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
try {
s = new Socket("localhost", 3000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setSize(400, 400);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
while (true) {
String tmp = dis.readUTF();
txaContent.append(tmp+"\n");
if(tmp.trim().equals("bye"))
{
s.close();break;
}
}
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientChat c = new ClientChat();
new Thread(c).start();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上很多的,比较专业的网站csdn ,我一直用,注册个帐号就可以,或者你可以找你的学长,估计他们做过了,我以前也是找的前辈的代码,这个最直接了,嘿嘿!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询