import java.io.*;
import java.net.*;
/**
* Insert the type's description here.
* Creation date: (00-11-3 18:08:18)
* @author: Leon Qin
*/
public class TestServer {
public static void main(String args[]) {
ServerSocket ssServer=null;
Socket sSocket=null;
InputStream isIn=null;
OutputStream osOut=null; for(;;){
try {
ssServer=new ServerSocket(1234);
System.out.println("Waiting for request......\n");
sSocket=ssServer.accept();
isIn=sSocket.getInputStream();
String sSocketIn=(new DataInputStream(isIn)).readUTF();
System.out.println(sSocketIn);
osOut=sSocket.getOutputStream();
(new DataOutputStream(osOut)).writeUTF("I got it!\n");
System.out.println("Answered!\n");
}
catch(Exception e){}
}
}
}客户端:
import java.io.*;
import java.net.*;
/**
* Insert the type's description here.
* Creation date: (00-11-3 18:08:18)
* @author: Leon Qin
*/
public class TestClient {
public static void main(String args[]) {
Socket sClient = null;
OutputStream osClient = null;
InputStream isClient = null;
InputStreamReader isrUserIn = new InputStreamReader(System.in);
BufferedReader brUserIn = new BufferedReader(isrUserIn);
String sUserIn = null;
for (;;) {
try {
sUserIn = brUserIn.readLine();
sClient = new Socket("127.0.0.1", 1234);
osClient = sClient.getOutputStream();
(new DataOutputStream(osClient)).writeUTF(sUserIn);
isClient = sClient.getInputStream();
System.out.println((new DataInputStream(isClient).readUTF()));
} catch (Exception e) {
}
}
}
}这样子不知道行不行。
import java.net.*;
/**
* Insert the type's description here.
* Creation date: (00-11-3 18:08:18)
* @author: Leon Qin
*/
public class TestServer {
public static void main(String args[]) {
ServerSocket ssServer=null;
Socket sSocket=null;
InputStream isIn=null;
OutputStream osOut=null; for(;;){
try {
ssServer=new ServerSocket(1234);
System.out.println("Waiting for request......\n");
sSocket=ssServer.accept();
isIn=sSocket.getInputStream();
String sSocketIn=(new DataInputStream(isIn)).readUTF();
System.out.println(sSocketIn);
osOut=sSocket.getOutputStream();
(new DataOutputStream(osOut)).writeUTF("I got it!\n");
System.out.println("Answered!\n");
}
catch(Exception e){}
}
}
}客户端:
import java.io.*;
import java.net.*;
/**
* Insert the type's description here.
* Creation date: (00-11-3 18:08:18)
* @author: Leon Qin
*/
public class TestClient {
public static void main(String args[]) {
Socket sClient = null;
OutputStream osClient = null;
InputStream isClient = null;
InputStreamReader isrUserIn = new InputStreamReader(System.in);
BufferedReader brUserIn = new BufferedReader(isrUserIn);
String sUserIn = null;
for (;;) {
try {
sUserIn = brUserIn.readLine();
sClient = new Socket("127.0.0.1", 1234);
osClient = sClient.getOutputStream();
(new DataOutputStream(osClient)).writeUTF(sUserIn);
isClient = sClient.getInputStream();
System.out.println((new DataInputStream(isClient).readUTF()));
} catch (Exception e) {
}
}
}
}这样子不知道行不行。
1.首先实现一个类.如:class mysocket extends java.util.Observable在该类中建立一个线程侦听套接口
2.对套接口感兴趣的类应实现java.util.Observer接口.
3.调用_mysocket.addObservable(_ILike);即可
可参见sun的联机帮助1.2