public class MyServer extends JFrame
{//服务器窗口
/**
*
*/
private static final long serialVersionUID = 3824477115998675420L;
JScrollPane jp;
JTextArea jt;
ServerSocket m_Listensocket;//监听端口请求
Socket Connect;
InetAddress ClientAddress;
@SuppressWarnings("deprecation")
public MyServer(int port,int count)
{
this.setTitle("服务器监听建立Socket通信窗口");
this.addWindowListener(new winAdpt());
try {
m_Listensocket=new ServerSocket(port,count);
jt=new JTextArea("Message:服务器已经启动!\n",10,50);
jp=new JScrollPane(jt);
this.add(jp);
this.setResizable(false);
pack();
show();
while(true)
{
Connect=m_Listensocket.accept();//不断监听端口请求,没有请求的话程序会阻塞在这里
ClientAddress=Connect.getInetAddress();//获得请求端的IP
jt.append("Message:与客户端建立Socket通信\n"+"Client From:"+ClientAddress.toString()+"\n");
MyService myservice=new MyService(this,this.Connect);
myservice.CommuciateThread.start();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}}
class winAdpt extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
((JFrame)e.getWindow()).dispose();
System.exit(0);
}}
package eclipse.scoket编程.服务器端程序;
public class MyService extends JFrame implements ActionListener,Runnable
{
MyServer mserver;//服务器
Socket ConnectedSocket;
Thread CommuciateThread;
String msgfromCli;
PrintStream os;
DataInputStream in;
JTextArea ta1,ta2;
JTextField t1;
JScrollPane sp1,sp2;
JPanel p1,p2,p3;
JButton jb1;
InetAddress ClientAddress;
public MyService(MyServer ms,Socket socket)
{
super("回复客户端");
mserver=ms;
ConnectedSocket=socket;
ClientAddress= ConnectedSocket.getInetAddress();
CommuciateThread=new Thread("通信线程");
this.setLayout(new BorderLayout());
ta1=new JTextArea(10,50);
ta2=new JTextArea(4,50);
ta2.requestFocus();
sp1=new JScrollPane(ta1);
this.add(sp1,"Center");
sp2=new JScrollPane(ta2);
jb1=new JButton("发送");
jb1.addActionListener(this);
p1=new JPanel();
p1.add(sp2);
t1=new JTextField(30);
t1.setText(ClientAddress.toString());
p2=new JPanel();
p2.add(t1);
p2.add(jb1);
p3=new JPanel();
p3.setLayout(new GridLayout(2,1));
p3.add(p1);
p3.add(p2);
this.add(p3,"South");
this.setResizable(false);
this.setSize(480,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try {
os=new PrintStream(new BufferedOutputStream(ConnectedSocket.getOutputStream()));
System.out.println(ConnectedSocket.toString());
if(e.getSource()==jb1)
{
os.println(ta2.getText());//把信息写入Scoket
os.flush();
ta1.append("Answer Client:"+ta2.getText()+"\n");
ta2.setText(null);
}
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//向客户端写入的流对象
// TODO 自动生成方法存根
// TODO 自动生成方法存根
} @SuppressWarnings("deprecation")
public void run()
{
try {
in=new DataInputStream(new BufferedInputStream(ConnectedSocket.getInputStream()));
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//从socket中读数据的流对象
try {
//msgfromCli=in.readLine();
while(!msgfromCli.equals("Bye"))
{ msgfromCli=in.readLine();
ta1.append("Client Say:"+msgfromCli+"\n");
}package eclipse.scoket编程.客户端程序;
public class MyClient extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -8159981553331388144L;
Socket ClientScoket;
String ip="192.168.1.101";
public final static int port=6666;
String msgfromser;
PrintStream os;
DataInputStream in;
JTextArea ta1,ta2;
JTextField t1;
JScrollPane sp1,sp2;
JPanel p1,p2,p3;
JButton jb1,jb2;
public MyClient()
{
super("客户端");
this.setLayout(new BorderLayout());
this.addWindowListener(new WinAdpt(this));
ta1=new JTextArea(10,50);
ta2=new JTextArea(4,50);
ta2.requestFocus();
sp1=new JScrollPane(ta1);
this.add(sp1,"Center");
sp2=new JScrollPane(ta2);
jb2=new JButton("发送");
jb2.addActionListener(this);
p1=new JPanel();
p1.add(sp2);
//p1.add(jb2);
t1=new JTextField(30);
t1.setText(ip);
jb1=new JButton("连接");
jb1.addActionListener(this);
p2=new JPanel();
p2.add(t1);
p2.add(jb1);
p2.add(jb2);
p3=new JPanel();
p3.setLayout(new GridLayout(2,1));
p3.add(p1);
p3.add(p2);
this.add(p3,"South");
this.setResizable(false);
this.setSize(480,400);
this.setVisible(true);
}
@SuppressWarnings("deprecation")
public void CreateConnect()
{
try {
ClientScoket=new Socket(t1.getText(),port);
System.out.print(ClientScoket.toString());
if(ClientScoket!=null)ta1.append(" 连接建立成功!"+"\n");
else ta1.append("连接失败!");
in=new DataInputStream(new BufferedInputStream(ClientScoket.getInputStream()));
msgfromser=in.readLine();//通过Scoket从service端读数据
//
System.out.print("所读数据:"+msgfromser+"ok");
ta1.append("Answer Client:"+msgfromser+"\n");
os=new PrintStream(new BufferedOutputStream(ClientScoket.getOutputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
CreateConnect();
System.out.print("已连接上");
}
if(e.getSource()==jb2)
{
os.print(ta2.getText());//把信息写入Scoket
os.flush();
ta1.append("Client Say:"+ta2.getText()+"\n");
ta2.setText(null);
}
}
public static void main(String args[]){new MyClient();}
}
//关闭窗口前向Server发送Bye在关闭输入输出流,连接
class WinAdpt extends WindowAdapter
{
MyClient myclient;
public WinAdpt(MyClient mc)
{
myclient=mc;
}
public void windowClosing(WindowEvent e)
{
try {
//myclient.os.println("Bye");
myclient.os.flush();
myclient.in.close();
myclient.os.close();
myclient.ClientScoket.close();
myclient.dispose();
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
ConnectedSocket.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
// TODO 自动生成方法存根
mserver.jt.append("Message:与客户端断开连接"+"\n");//告诉服务器连接断开
dispose();//关闭当前与Client通信的Frame
}}
package eclipse.scoket编程.服务器端程序;public class TestServer
{
public static void main(String args[])
{
MyServer myser=new MyServer(6666,10);
}
}
{//服务器窗口
/**
*
*/
private static final long serialVersionUID = 3824477115998675420L;
JScrollPane jp;
JTextArea jt;
ServerSocket m_Listensocket;//监听端口请求
Socket Connect;
InetAddress ClientAddress;
@SuppressWarnings("deprecation")
public MyServer(int port,int count)
{
this.setTitle("服务器监听建立Socket通信窗口");
this.addWindowListener(new winAdpt());
try {
m_Listensocket=new ServerSocket(port,count);
jt=new JTextArea("Message:服务器已经启动!\n",10,50);
jp=new JScrollPane(jt);
this.add(jp);
this.setResizable(false);
pack();
show();
while(true)
{
Connect=m_Listensocket.accept();//不断监听端口请求,没有请求的话程序会阻塞在这里
ClientAddress=Connect.getInetAddress();//获得请求端的IP
jt.append("Message:与客户端建立Socket通信\n"+"Client From:"+ClientAddress.toString()+"\n");
MyService myservice=new MyService(this,this.Connect);
myservice.CommuciateThread.start();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}}
class winAdpt extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
((JFrame)e.getWindow()).dispose();
System.exit(0);
}}
package eclipse.scoket编程.服务器端程序;
public class MyService extends JFrame implements ActionListener,Runnable
{
MyServer mserver;//服务器
Socket ConnectedSocket;
Thread CommuciateThread;
String msgfromCli;
PrintStream os;
DataInputStream in;
JTextArea ta1,ta2;
JTextField t1;
JScrollPane sp1,sp2;
JPanel p1,p2,p3;
JButton jb1;
InetAddress ClientAddress;
public MyService(MyServer ms,Socket socket)
{
super("回复客户端");
mserver=ms;
ConnectedSocket=socket;
ClientAddress= ConnectedSocket.getInetAddress();
CommuciateThread=new Thread("通信线程");
this.setLayout(new BorderLayout());
ta1=new JTextArea(10,50);
ta2=new JTextArea(4,50);
ta2.requestFocus();
sp1=new JScrollPane(ta1);
this.add(sp1,"Center");
sp2=new JScrollPane(ta2);
jb1=new JButton("发送");
jb1.addActionListener(this);
p1=new JPanel();
p1.add(sp2);
t1=new JTextField(30);
t1.setText(ClientAddress.toString());
p2=new JPanel();
p2.add(t1);
p2.add(jb1);
p3=new JPanel();
p3.setLayout(new GridLayout(2,1));
p3.add(p1);
p3.add(p2);
this.add(p3,"South");
this.setResizable(false);
this.setSize(480,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try {
os=new PrintStream(new BufferedOutputStream(ConnectedSocket.getOutputStream()));
System.out.println(ConnectedSocket.toString());
if(e.getSource()==jb1)
{
os.println(ta2.getText());//把信息写入Scoket
os.flush();
ta1.append("Answer Client:"+ta2.getText()+"\n");
ta2.setText(null);
}
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//向客户端写入的流对象
// TODO 自动生成方法存根
// TODO 自动生成方法存根
} @SuppressWarnings("deprecation")
public void run()
{
try {
in=new DataInputStream(new BufferedInputStream(ConnectedSocket.getInputStream()));
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//从socket中读数据的流对象
try {
//msgfromCli=in.readLine();
while(!msgfromCli.equals("Bye"))
{ msgfromCli=in.readLine();
ta1.append("Client Say:"+msgfromCli+"\n");
}package eclipse.scoket编程.客户端程序;
public class MyClient extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -8159981553331388144L;
Socket ClientScoket;
String ip="192.168.1.101";
public final static int port=6666;
String msgfromser;
PrintStream os;
DataInputStream in;
JTextArea ta1,ta2;
JTextField t1;
JScrollPane sp1,sp2;
JPanel p1,p2,p3;
JButton jb1,jb2;
public MyClient()
{
super("客户端");
this.setLayout(new BorderLayout());
this.addWindowListener(new WinAdpt(this));
ta1=new JTextArea(10,50);
ta2=new JTextArea(4,50);
ta2.requestFocus();
sp1=new JScrollPane(ta1);
this.add(sp1,"Center");
sp2=new JScrollPane(ta2);
jb2=new JButton("发送");
jb2.addActionListener(this);
p1=new JPanel();
p1.add(sp2);
//p1.add(jb2);
t1=new JTextField(30);
t1.setText(ip);
jb1=new JButton("连接");
jb1.addActionListener(this);
p2=new JPanel();
p2.add(t1);
p2.add(jb1);
p2.add(jb2);
p3=new JPanel();
p3.setLayout(new GridLayout(2,1));
p3.add(p1);
p3.add(p2);
this.add(p3,"South");
this.setResizable(false);
this.setSize(480,400);
this.setVisible(true);
}
@SuppressWarnings("deprecation")
public void CreateConnect()
{
try {
ClientScoket=new Socket(t1.getText(),port);
System.out.print(ClientScoket.toString());
if(ClientScoket!=null)ta1.append(" 连接建立成功!"+"\n");
else ta1.append("连接失败!");
in=new DataInputStream(new BufferedInputStream(ClientScoket.getInputStream()));
msgfromser=in.readLine();//通过Scoket从service端读数据
//
System.out.print("所读数据:"+msgfromser+"ok");
ta1.append("Answer Client:"+msgfromser+"\n");
os=new PrintStream(new BufferedOutputStream(ClientScoket.getOutputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
CreateConnect();
System.out.print("已连接上");
}
if(e.getSource()==jb2)
{
os.print(ta2.getText());//把信息写入Scoket
os.flush();
ta1.append("Client Say:"+ta2.getText()+"\n");
ta2.setText(null);
}
}
public static void main(String args[]){new MyClient();}
}
//关闭窗口前向Server发送Bye在关闭输入输出流,连接
class WinAdpt extends WindowAdapter
{
MyClient myclient;
public WinAdpt(MyClient mc)
{
myclient=mc;
}
public void windowClosing(WindowEvent e)
{
try {
//myclient.os.println("Bye");
myclient.os.flush();
myclient.in.close();
myclient.os.close();
myclient.ClientScoket.close();
myclient.dispose();
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
ConnectedSocket.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
// TODO 自动生成方法存根
mserver.jt.append("Message:与客户端断开连接"+"\n");//告诉服务器连接断开
dispose();//关闭当前与Client通信的Frame
}}
package eclipse.scoket编程.服务器端程序;public class TestServer
{
public static void main(String args[])
{
MyServer myser=new MyServer(6666,10);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货