import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Event;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
public class myFrame1 extends JFrame implements ActionListener, Runnable
{
JButton b1,b2;
JTextField t1;
JTextArea a1;
JPanel p1,p2;
ServerSocket server = null;
Socket clientSocket;
Thread c;
//向客户器端发送信息的线程
BufferedReader sin;
PrintWriter sout;
public myFrame1()
{
setSize(400,400);
setLocation(250,456);
b1=new JButton("发送"
;
b2=new JButton("结束"
;
b1.addActionListener(this);
b2.addActionListener(this);
t1=new JTextField(10);
a1=new JTextArea();
p1=new JPanel();
p2=new JPanel();
p1.add(a1);
p1.setLayout(new GridLayout(1,1));
p2.add(t1);
p2.add(b1);
p2.add(b2);
p2.setLayout(new GridLayout(1,3));
getContentPane().add("South",p2);
getContentPane().add("Center",p1);
}
public void actionPerformed(ActionEvent e)
{
// TODO 自动生成方法存根
if(e.getSource()==b1)
{
try
{
server = new ServerSocket(5678);
System.out.println("端口号:"+server.getLocalPort());
clientSocket = server.accept();
System.out.println("已建立连接!"
;
InputStream is = clientSocket.getInputStream();
sin = new BufferedReader(new InputStreamReader(is));
OutputStream os = clientSocket.getOutputStream();
sout = new PrintWriter(os);
c = new Thread(this);
c.start();
//启动线程,向客户器端发送信息
String aline;
while((aline=sin.readLine())!=null)
{ //接收客户端的数据
a1.append(aline);
if(aline.equals("bye"
)
{
c.interrupt();
//线程中断
break;
}
}
sout.close();
//关闭流
os.close();
sin.close();
is.close();
clientSocket.close(); //关闭Socket连接
System.exit(0);
//程序运行结束
}
catch(Exception e1)
{
System.out.println(e1);
}
}
else
if(e.getSource()==b2)
{
try
{
server.close();
//停止ServerSocket服务
}
catch(IOException e2)
{
System.out.println(e2);
}
}
}
public void run() {
// TODO 自动生成方法存根
while(true)
{
try
{
int ch;
while((ch=System.in.read())!=-1)
{ //从键盘接收字符并向客户端发送
sout.write((byte)ch);
if(ch=='/n')
sout.flush();
//将缓冲区内容向输出流发送
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
myFrame1 m1=new myFrame1();
m1.setTitle("聊天工具服务器"
;
m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m1.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Event;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
public class myFrame extends JFrame implements ActionListener,Runnable
{
JButton b1,b2;
JTextField t1;
JTextArea a1;
JPanel p1,p2;
Socket clientSocket;
Thread c;
//用于向服务器端发送信息
BufferedReader cin;
PrintWriter cout;
public myFrame()
{
setSize(400,400);
setLocation(250,456);
b1=new JButton("发送"
;
b2=new JButton("结束"
;
b1.addActionListener(this);
b2.addActionListener(this);
t1=new JTextField(10);
a1=new JTextArea();
p1=new JPanel();
p2=new JPanel();
p1.add(a1);
p1.setLayout(new GridLayout(1,1));
p2.add(t1);
p2.add(b1);
p2.add(b2);
p2.setLayout(new GridLayout(1,3));
getContentPane().add("South",p2);
getContentPane().add("Center",p1);
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(e.getSource()==b1)
{
try
{
clientSocket=new Socket("localhost",5678);//连接服务器端,这里使用本机
System.out.println("已建立连接!"
;
InputStream is = clientSocket.getInputStream();
cin=new BufferedReader(new InputStreamReader(is));
OutputStream os = clientSocket.getOutputStream();
cout = new PrintWriter(os);
c = new Thread(this);
c.start();
//启动线程,向服务器端发送信息
String aline;
while((aline=cin.readLine())!=null)
{ //接收服务器端的数据
a1.append(aline);
if(aline.equals("bye"
)
{
c.interrupt();
break;
}
}
cout.close();
os.close();
cin.close();
is.close();
clientSocket.close();
//关闭Socket连接
System.exit(0);
}
catch(Exception e1)
{
System.out.println(e1);
}
}
if(e.getSource()==b2)
{
System.exit(0);
}
}
public void run()
{
while(true)
{
try
{ //从键盘接收字符并向服务器端发送
int ch;
while((ch=System.in.read())!=-1)
{
cout.write((byte)ch);
if(ch=='/n')
cout.flush();
//将缓冲区内容向输出流发送
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
myFrame m1=new myFrame();
m1.setTitle("聊天工具"
;
m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m1.setVisible(true);
}
}