62,623
社区成员
发帖
与我相关
我的任务
分享
//服务器端
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame
{
JTextArea txt;
public void serve()
{
txt=new JTextArea();
this.setTitle("学习通讯类");
this.setLayout(new BorderLayout());
this.add(txt,BorderLayout.CENTER);
this.setSize(500,300);
this.setVisible(true);
try
{
ServerSocket seSocket=new ServerSocket(8000);
txt.append("信息接受时间是:"+new Date()+'\n');
Socket st=seSocket.accept();
DataInputStream in=new DataInputStream(st.getInputStream());
while(true)
{
String Dialog=in.readLine();
txt.append("从客户端收到信息"+Dialog+'\n');
}
}
catch(IOException ex)
{
System.err.println(ex);
}
}
public static void main(String args[])
{
Server myserver=new Server();
myserver.serve();
}
}
//客户端
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class Client extends JFrame implements ActionListener
{
JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
DataOutputStream out;
Container f=this.getContentPane();
public void client()
{
f.setLayout(new BorderLayout());
txta=new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf=new JTextField(20);
bt=new JButton("发送");
pl=new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
try
{
Socket sc=new Socket("192.168.0.25",8000);
out=new DataOutputStream(sc.getOutputStream());
}
catch(IOException ex)
{
txta.append(ex.toString()+'\n');
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt)
{
String Dialog=txtf.getText();
try
{
out.writeChars(Dialog);
out.flush();
txta.append("已经发送对话:"+Dialog+'\n');
}
catch(IOException ex)
{
txta.append(ex.toString()+'\n');
}
}
}
public static void main(String args[])
{
Client myclient=new Client();
myclient.client();
}
}
import java.net.*;
import java.io.*;
class NetTest extends Thread
{
public static void main(String [] args)
{
if (args.length>0)
{
server();
}
else
{
client();
}
}
private Socket s;
public NetTest(Socket s)
{
this.s=s;
}
public void run()
{
try
{
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
os.write("server".getBytes());
byte [] buf=new byte[100];
int len =is.read(buf);
System.out.println(new String(buf,0,len) );
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void server()
{
try
{
ServerSocket ss=new ServerSocket(6000);
while(true)
{
Socket s=ss.accept();
new NetTest(s).start();
//ss.close();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void client()
{
try
{
Socket s=new Socket(InetAddress.getByName(null),6000);
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
byte [] buf=new byte[100];
int len =is.read(buf);
System.out.println(new String(buf,0,len) );
os.write("client".getBytes());
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println("网络不通");
}
}
}