WEB开发网
开发学院WEB开发Jsp 关于java socket的问题 阅读

关于java socket的问题

 2008-01-05 09:31:55 来源:WEB开发网   
核心提示:要做成这样的:客户端访问服务器ip和port,服务器马上发送welcome消息,关于java socket的问题,然后客户端根据welcome消息判定是否是想要连接的服务器和端口,假如是则开始发送数据,数据包括开始和结束的校验,服务器端发送welcome消息后开始接收数据

要做成这样的:

客户端访问服务器ip和port,服务器马上发送welcome消息,然后客户端根据welcome消息判定是否是想要连接的服务器和端口,

假如是则开始发送数据,数据包括开始和结束的校验。服务器端发送welcome消息后开始接收数据,直到结束。蛮简单的事TMD我怎么就搞不定。

CLIENT:

package com.ces.mytest.socket;

import java.net.*;import java.io.*;

public class ClientThread extends Thread {   PRivate String ipStr = "";   private int port;   public ClientThread(String ipStr, int port) {     this.ipStr = ipStr;     this.port = port;   }

   public void run() {     try {       Socket client = new Socket(ipStr, port);       BufferedReader read = new BufferedReader(new InputStreamReader(client.getInputStream()));       BufferedWriter write = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

       if ("欢迎".equals(read.readLine())) {         System.out.println("校验正确");

         write.write("开始");         write.newLine();         write.write("数据");         write.newLine();         write.write("结束");         write.newLine();         write.flush();

         System.out.println("发送完毕");       }

       write.close();       read.close();       client.close();     } catch (UnknownHostException ex) {       ex.printStackTrace();     } catch (IOException ex) {       ex.printStackTrace();     }   }

   public static void main(String[] args) {     Thread client = new ClientThread("127.0.0.1",10020);     client.start();   }

}

SERVER:

package com.ces.mytest.socket;

import java.net.*;import java.io.*;

public class ServerThread extends Thread {   private int port;   public ServerThread(int port) {     this.port = port;   }

   private boolean stop = false;

   public void run() {     try {       ServerSocket server = new ServerSocket(port);       while (!stop) {         Socket client = server.accept();         BufferedReader read = new BufferedReader(new InputStreamReader(client.getInputStream()));         BufferedWriter write = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));                  System.out.println("发送欢迎信息");                  write.write("欢迎");         write.flush();

//         String lineStr = read.readLine();//         if ("开始".equals(lineStr)) {//           System.out.println("开始接受数据!");////           while (true) {//             lineStr = read.readLine();//             if ("结束".equals(lineStr)) {//               break;//             } else {//               System.out.println("数据是" + lineStr);//             }//           }//         }

         write.close();         read.close();         client.close();       }

     } catch (IOException ex) {       ex.printStackTrace();     }   }

   public static void main(String[] args) {     Thread server = new ServerThread(10020);     server.start();   }

   public boolean isStop() {     return stop;   }

   pu

Tags:关于 java socket

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接