使用流套接字来示例一个客户/服务器的应用。客户端的应用连接到服务器上面,服务端的应用发送数据到客户端,然后客户将收到的数据显示出来。
服务端代码:
package socket; import java.awt.Event; import java.awt.Frame; import java.awt.TextArea; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server extends Frame { TextArea display; public Server() { super("Server"); display = new TextArea(20, 5); add("Center", display); resize(300, 150); show(); } public void runServer(){ ServerSocket server; Socket connection; OutputStream output; PrintWriter pw = null; try { /*建立一个ServerSocket,第一个参数是端口port,第二个参数是是队列长度queueLength,即规定了服务器能够处理的 等待连接的客户数目,如果满了,将自动拒绝客户的请求 */ server = new ServerSocket(5000,100); //等待请求,此方法会一直阻塞,直到获得请求才往下走 connection = server.accept(); display.setText("connection received... "); display.setText("Sending data... "); //获得和Socket相关联的OutputStream的一个引用 output = connection.getOutputStream(); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())),true); String s = new String("Connection successful "); // //逐个字符发送 // for(int i = 0;i<s.length();i++){ // //进行单一字节或字节组合的发送和接收 // output.write((int)s.charAt(i)); // } pw.println(s); display.appendText("Transmission complete. Closing socket. "); //关闭socket connection.close(); pw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //保证用户在应用结束的时候关闭相应的窗口 public boolean handleEvent(Event e){ if(e.id == Event.WINDOW_DESTROY){ hide(); dispose(); System.exit(0); } return super.handleEvent(e); } public static void main(String args[]){ Server s = new Server(); s.runServer(); } }
客户端代码:
package socket; import java.awt.Event; import java.awt.Frame; import java.awt.TextArea; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class Client extends Frame{ TextArea display; public Client() { super("Client"); display = new TextArea(20, 10); add("Center", display); resize(300, 150); show(); } public void runClient(){ Socket client; InputStream input; BufferedReader br = null; try { client = new Socket(InetAddress.getLocalHost(),5000); display.appendText("Created Socket "); input = client.getInputStream(); br = new BufferedReader(new InputStreamReader( client.getInputStream())); display.appendText("Created input stream "); display.appendText("The text from the server is: "); char c; //逐个字符读取数据 // while((c = (char)input.read())!=' '){ // display.appendText(String.valueOf(c)); // } String str = br.readLine(); display.appendText(str); client.close(); br.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean handleEvent(Event e){ if(e.id == Event.WINDOW_DESTROY){ hide(); dispose(); System.exit(0); } return super.handleEvent(e); } public static void main(String[] args) { // TODO Auto-generated method stub Client c = new Client(); c.runClient(); } }