//Server_TCP.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;public class Server_TCP extends WindowAdapter
implements ActionListener, KeyListener {
TextField str_send;
Label label;
TextArea msg;
Button send,exit;
Panel pl; boolean started = false;
ServerSocket ss = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
//设置接收数据报包和发送数据报包
public void display ( ) {
Frame f=new Frame ("在线聊天---服务器");
f.setSize(400,350);
f.setLocation(400,400);
f.setBackground(Color.cyan);
pl = new Panel( );
f.add(pl,"South");
msg = new TextArea ( );
msg.setSize (100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
f.add(msg);
label=new Label ("发送消息");
pl.add(label);
str_send = new TextField(20);
pl.add(str_send);
str_send.addKeyListener(this);
send = new Button("发送");
pl.add(send);
send.addActionListener(this);
exit = new Button("退出");
pl.add(exit);
exit.addActionListener(this);
f.addWindowListener(this);
f.setVisible(true);
try ////设置服务器发送端口
{
ss = new ServerSocket(2222);
started = true;
}catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
msg.append("a client connected!"+"\n");
bConnected = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void receiveMessage( ) { //接收数据报包并显示
try
{
while (bConnected)
{
String str = dis.readUTF(); msg.append("Client:" + str + '\n');
}
}
catch (Exception e)
{
msg.append(e+"\n");
}
} public void sendMessage( ){
String str = str_send.getText().trim();
//taContent.setText(str);
msg.append("Server:"+str+"\n");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
str_send.setText("");
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource( )==send)
{
sendMessage( );
}
if (e.getSource( )==exit)
{
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
}
} public void windowCloseing(WindowEvent e) {
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
} public void keyPressed(KeyEvent e) {
if (e.getSource( )==str_send)
{
if (e.getKeyChar( )== KeyEvent.VK_ENTER)
{
sendMessage( );//发送数据报包
}
}
} public static void main(String arg[ ]) {
Server_TCP app=new Server_TCP( );
app.display( );
app.receiveMessage( );
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
//Client_TCP.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;public class Client_TCP extends WindowAdapter
implements ActionListener, KeyListener {
TextField str_send;
Label label;
TextArea msg;
Button send,exit;
Panel pl; Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
//设置接收数据报包和发送数据报包
public void display ( ) {
Frame f=new Frame ("在线聊天---客户端");
f.setSize(400,350);
f.setLocation(100,100);
f.setBackground(Color.cyan);
pl = new Panel( );
f.add(pl,"South");
msg = new TextArea ( );
msg.setSize (100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
f.add(msg);
label=new Label ("发送消息");
pl.add(label);
str_send = new TextField(20);
pl.add(str_send);
str_send.addKeyListener(this);
send = new Button("发送");
pl.add(send);
send.addActionListener(this);
exit = new Button("退出");
pl.add(exit);
exit.addActionListener(this);
f.addWindowListener(this);
f.setVisible(true);
try
{ connect();
}catch(Exception e){
msg.append(e +"\n");
}
}
public void connect() {
try {
s = new Socket("127.0.0.1", 2222);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
msg.append("connected!"+"\n");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
public void receiveMessage( ) { //接收数据报包并显示
try
{
while (bConnected)
{
String str = dis.readUTF();
//System.out.println(str);
msg.append("Server:" + str + '\n');
}
}
catch (Exception e)
{
msg.append(e+"\n");
}
} public void sendMessage ( ) {
String str = str_send.getText().trim();
System.out.println(str);
//taContent.setText(str);
msg.append("Client:"+str+"\n");
System.out.println();
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
str_send.setText("");
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource( )==send)
{
sendMessage( );//发送数据包
}
if (e.getSource( )==exit)
{
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
}
} public void windowCloseing(WindowEvent e) {
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
} public void keyPressed(KeyEvent e) {
if (e.getSource( )==str_send)
{
if (e.getKeyChar( )== KeyEvent.VK_ENTER)
{
sendMessage( );//发送数据报包
}
}
} public static void main(String arg[ ]) {
Client_TCP client=new Client_TCP( );
client.display( );
client.receiveMessage( );
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
服务器tcp
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;public class Server_TCP extends WindowAdapter
implements ActionListener, KeyListener {
TextField str_send;
Label label;
TextArea msg;
Button send,exit;
Panel pl; boolean started = false;
ServerSocket ss = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
//设置接收数据报包和发送数据报包
public void display ( ) {
Frame f=new Frame ("在线聊天---服务器");
f.setSize(400,350);
f.setLocation(400,400);
f.setBackground(Color.cyan);
pl = new Panel( );
f.add(pl,"South");
msg = new TextArea ( );
msg.setSize (100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
f.add(msg);
label=new Label ("发送消息");
pl.add(label);
str_send = new TextField(20);
pl.add(str_send);
str_send.addKeyListener(this);
send = new Button("发送");
pl.add(send);
send.addActionListener(this);
exit = new Button("退出");
pl.add(exit);
exit.addActionListener(this);
f.addWindowListener(this);
f.setVisible(true);
try ////设置服务器发送端口
{
ss = new ServerSocket(2222);
started = true;
}catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
msg.append("a client connected!"+"\n");
bConnected = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void receiveMessage( ) { //接收数据报包并显示
try
{
while (bConnected)
{
String str = dis.readUTF(); msg.append("Client:" + str + '\n');
}
}
catch (Exception e)
{
msg.append(e+"\n");
}
} public void sendMessage( ){
String str = str_send.getText().trim();
//taContent.setText(str);
msg.append("Server:"+str+"\n");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
str_send.setText("");
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource( )==send)
{
sendMessage( );
}
if (e.getSource( )==exit)
{
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
}
} public void windowCloseing(WindowEvent e) {
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
} public void keyPressed(KeyEvent e) {
if (e.getSource( )==str_send)
{
if (e.getKeyChar( )== KeyEvent.VK_ENTER)
{
sendMessage( );//发送数据报包
}
}
} public static void main(String arg[ ]) {
Server_TCP app=new Server_TCP( );
app.display( );
app.receiveMessage( );
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
//Client_TCP.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;public class Client_TCP extends WindowAdapter
implements ActionListener, KeyListener {
TextField str_send;
Label label;
TextArea msg;
Button send,exit;
Panel pl; Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
//设置接收数据报包和发送数据报包
public void display ( ) {
Frame f=new Frame ("在线聊天---客户端");
f.setSize(400,350);
f.setLocation(100,100);
f.setBackground(Color.cyan);
pl = new Panel( );
f.add(pl,"South");
msg = new TextArea ( );
msg.setSize (100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
f.add(msg);
label=new Label ("发送消息");
pl.add(label);
str_send = new TextField(20);
pl.add(str_send);
str_send.addKeyListener(this);
send = new Button("发送");
pl.add(send);
send.addActionListener(this);
exit = new Button("退出");
pl.add(exit);
exit.addActionListener(this);
f.addWindowListener(this);
f.setVisible(true);
try
{ connect();
}catch(Exception e){
msg.append(e +"\n");
}
}
public void connect() {
try {
s = new Socket("127.0.0.1", 2222);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
msg.append("connected!"+"\n");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
public void receiveMessage( ) { //接收数据报包并显示
try
{
while (bConnected)
{
String str = dis.readUTF();
//System.out.println(str);
msg.append("Server:" + str + '\n');
}
}
catch (Exception e)
{
msg.append(e+"\n");
}
} public void sendMessage ( ) {
String str = str_send.getText().trim();
System.out.println(str);
//taContent.setText(str);
msg.append("Client:"+str+"\n");
System.out.println();
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
str_send.setText("");
}
//实现ActionListener接口中的方法
public void actionPerformed(ActionEvent e) {
if (e.getSource( )==send)
{
sendMessage( );//发送数据包
}
if (e.getSource( )==exit)
{
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
}
} public void windowCloseing(WindowEvent e) {
System.out.println("聊天程序已关闭,再见!\n");
System.exit(0);
} public void keyPressed(KeyEvent e) {
if (e.getSource( )==str_send)
{
if (e.getKeyChar( )== KeyEvent.VK_ENTER)
{
sendMessage( );//发送数据报包
}
}
} public static void main(String arg[ ]) {
Client_TCP client=new Client_TCP( );
client.display( );
client.receiveMessage( );
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
服务器tcp
Socket s = ss.accept();
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
msg.append("a client connected!"+"\n");
bConnected = true;}
之间加一句started=false;就可以了