serve:
package Test;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ServerSocket sock = new ServerSocket(3000);
System.out.println("The server is start........");
while(true)
{
Socket s = sock.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello, This is a test from Server");
ps.close();
s.close();
}
}
}
client:
package Test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Socket s = new Socket("127.0.0.1", 3000);
BufferedReader br = new BufferedReader( //convert byte stream into char stream
new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.println("the recv data is: "+str);
br.close();
s.close();
}
}