62,623
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyServer extends Frame implements Runnable {
TextArea t;
Socket s;
ServerSocket ss;
DataOutputStream out;
Thread myThread;
public MyServer() {
t = new TextArea("这里是服务器\n", 10, 10, TextArea.SCROLLBARS_BOTH);
add(t, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
setBounds(200, 200, 400, 400);
setVisible(true);
validate();
connection();
}
public void connection() {
try {
ss = new ServerSocket(3400);
} catch (IOException e1) {
e1.printStackTrace();
}
while(true) {
try {
s = ss.accept();
if (s != null) {
new Thread(this).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
try {
out = new DataOutputStream(s.getOutputStream());
byte buffer[] = "程序已经成功!".getBytes();
out.write(buffer, 0, buffer.length);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new MyServer();
}
}
MyClient.java
[code=Java]
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyClient extends Frame implements Runnable, ActionListener {
TextArea t;
Button b = new Button("连接");
Button b1 = new Button("断开");
Socket s;
DataInputStream in;
Thread myThread;
InetSocketAddress address;
boolean isEnd;
public MyClient() {
b1.addActionListener(this);
b.addActionListener(this);
add(b, "North");
add(b1, "South");
t = new TextArea("这里是一号机\n", 10, 10, TextArea.SCROLLBARS_BOTH);
add(t, "Center");
setBounds(200, 200, 400, 400);
setVisible(true);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
myThread = new Thread(this);
myThread.start();
} else if (e.getSource() == b1) {
// isEnd = true;
myThread = null;
try {
s.close();
t.append("连接已断开\n");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void run() {
// isEnd = false;
try {
address = new InetSocketAddress(InetAddress.getByName("127.0.0.1"),
3400);
s = new Socket();
s.connect(address, 5000);
if (!s.isConnected()) {
t.append("Haven't connected to the Server\n");
} else {
t.append("Have connected to the Server\n");
in = new DataInputStream(s.getInputStream());
int l = 0;
byte buffer[] = new byte[1024];
while ((l = in.read(buffer, 0, 1024)) != -1) {
t.append(new String(buffer, 0, l) + '\n');
// if (isEnd == true) {
// in.close();
// return;
// }
}
}
} catch (SocketTimeoutException e1) {
t.append("Haven't connected to the Server\n");
} catch (IOException e) {
}
}
public static void main(String args[]) {
new MyClient();
}
}
public void run()
{
try
{
ss = new ServerSocket( 3400 );
while( true )
{
s = ss.accept();
out = new DataOutputStream( s.getOutputStream() );
byte buffer[] = "程序已经成功!".getBytes();
out.write( buffer,0,buffer.length );
out.close();
}
}
catch( IOException e )
{
}
}
public static void main( String[] args )
{
Thread t = new Thread( new MyServer() );
t.start();
}