package testclass;import java.net.*;
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class TestServer extends Thread{
public TestServer() {
} public void run(){
try {
ServerSocket serverSocket = new ServerSocket(5555);
while (true){
Socket client = serverSocket.accept();
if (client != null){
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream(); byte[] buffer = new byte[10000];
if (in.read(buffer) > 0) {
System.out.println("read:" + new String(buffer).trim());
}
System.out.println("got it");
String report = "got you";
out.write(report.getBytes());
// Thread.sleep(1000);
out.flush();
client.close();
System.out.println("report ok");
//client.close();
Thread.sleep(50);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
TestServer testServer = new TestServer();
testServer.start();
}}
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class TestServer extends Thread{
public TestServer() {
} public void run(){
try {
ServerSocket serverSocket = new ServerSocket(5555);
while (true){
Socket client = serverSocket.accept();
if (client != null){
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream(); byte[] buffer = new byte[10000];
if (in.read(buffer) > 0) {
System.out.println("read:" + new String(buffer).trim());
}
System.out.println("got it");
String report = "got you";
out.write(report.getBytes());
// Thread.sleep(1000);
out.flush();
client.close();
System.out.println("report ok");
//client.close();
Thread.sleep(50);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
TestServer testServer = new TestServer();
testServer.start();
}}
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class TestClient extends Thread {
public TestClient() {
} public void run(){
try {
Socket client = new Socket("localhost", 5555);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write( ("hello").getBytes());
out.flush(); byte[] inputBytes = new byte[20000];
int readPos = 0;
int readNum = 0;
while((readNum=in.read(inputBytes,readPos,20000-readPos))>0){
readPos+=readNum;
}
System.out.println(new String(inputBytes).trim());
}
catch (Exception ex) {
ex.printStackTrace();
}
} public static void main(String[] args) {
TestClient testClient = new TestClient();
testClient.start();
}}
有多种方法可供选择:直接连接数据库/EJB/RMI等
客户端怎么运行你的程序?