public static void main(String[] args) { new Aserver();
}
class BListener implements ActionListener { public void actionPerformed(ActionEvent e) { int port=Integer.parseInt(t1.getText()); if(e.getActionCommand().equals("监听")) try{
class BListener implements ActionListener { public void actionPerformed(ActionEvent e) { String ip=t1.getText(); int port=Integer.parseInt(t2.getText());
import java.io.*;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Aserver extends JFrame{
Socket s=null;
ServerSocket ss;
JLabel l1=new JLabel("PORT");
JTextField t1=new JTextField(8);
JTextArea t2=new JTextArea(6,15);
JTextField t3=new JTextField(20);
JButton b1=new JButton("监听");
JButton b2=new JButton("发送");
JPanel p=new JPanel();
DataInputStream in;
DataOutputStream out;
String s1="";
public Aserver()
{
add(p);
p.add(l1);
p.add(t1);
p.add(b1);
p.add(t2);
p.add(t3);
p.add(b2);
b1.addActionListener(new BListener());
b2.addActionListener(new BListener());
this.setSize(300,260);
this.setLocation(320,140);
this.setVisible(true);
}
public static void main(String[] args)
{
new Aserver();
}
class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int port=Integer.parseInt(t1.getText());
if(e.getActionCommand().equals("监听"))
try{
ss=new ServerSocket(port);
t2.setText("监听的端口:"+port+"\n");
new Thread(){
public void run(){
try{
s=ss.accept();
t2.append("连接成功! 本地IP:"+s.getRemoteSocketAddress()+"\n");
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
while((s1=in.readUTF())!=null){
t2.append("客户端说:"+s1+"\n");
}
}
catch(IOException e) {
e.printStackTrace();
}
}
}.start();
System.out.println("连接成功");
}
catch(IOException ie)
{
ie.printStackTrace();
}
catch(NumberFormatException e1) {
e1.printStackTrace();
}
if(e.getActionCommand().equals("发送"))
try
{ out.writeUTF(t3.getText());
t2.append( "服务器说:"+t3.getText()+"\n");
System.out.println("发送成功");
}
catch(Exception ie)
{
ie.printStackTrace();
System.out.println("发送失败");
}
}
}
}
package serverclient;import java.net.*;
import java.io.*;import javax.swing.*;
//import java.awt.*;
import java.awt.event.*;
public class Aclient extends JFrame{
Socket s=null;
//ServerSocket ss;
JLabel l1=new JLabel("IP");
JLabel l2=new JLabel("PORT");
JTextField t1=new JTextField(8);
JTextField t2=new JTextField(6);
JTextArea t3=new JTextArea(6,15);
JTextField t4=new JTextField(20);
JButton b1=new JButton("连接");
JButton b2=new JButton("断开");
JButton b3=new JButton("发送");
JPanel p=new JPanel();
DataInputStream in;
DataOutputStream out;
String s1;
public Aclient()
{
add(p);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b1);
p.add(b2);
p.add(t3);
p.add(t4);
p.add(b3);
b1.addActionListener(new BListener());
b2.addActionListener(new BListener());
b3.addActionListener(new BListener());
this.setSize(400,250);
this.setLocation(320,140);
this.setVisible(true);
}
class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ip=t1.getText();
int port=Integer.parseInt(t2.getText());
if(e.getActionCommand().equals("连接"))
try
{
s=new Socket(ip,port);
t3.setText("连接成功 ! 本地IP:"+s.getRemoteSocketAddress()+"\n");
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
new Thread(){
public void run(){
try{
while((s1=in.readUTF())!=null){
t3.append("服务器说:"+s1+"\n");
}
}
catch(IOException e){
e.printStackTrace();
}
}
}.start();
System.out.println("连接成功");
}
catch(IOException ie)
{
ie.printStackTrace(); }
catch (NumberFormatException e1) {
e1.printStackTrace();
}
if(e.getActionCommand().equals("断开"))
try
{
s.close();
System.out.println("断开连接");
t3.append("断开连接!\n");
}
catch(IOException ie)
{
ie.printStackTrace();
}
if(e.getActionCommand().equals("发送"))
try
{
out.writeUTF(t4.getText());
t3.append( "客户端说:"+t4.getText()+"\n");
System.out.println("发送成功");
}
catch(Exception ie)
{
ie.printStackTrace();
System.out.println("发送失败");
}
}
}public static void main(String[] args)
{
new Aclient();}
}