帮帮忙,好吗,主要是socket和client双向通讯比较模糊

解决方案 »

  1.   

    看看Java 编程思想一书吧?
      

  2.   

    不知道对你有没有用class ThreadListen extends Thread{
    public void run(){
    try{
       sleep(100); 
         Chat.ServerListen();
    }catch(Exception e){
       e.printStackTrace();
    }   
    }
    }public class Chat extends Form
    {
    public static int PORT=8080;
    public Client client;

    public Chat(){
    super();
      initForm();

    ThreadListen threadlisten=new ThreadListen();
    threadlisten.start();
    client=new Client();
    } public static void ServerListen(){
    Server server=new Server();
    server.listen();
    } public static void main(String args[])
    {
    Application.run(new Chat());
    } public void dispose()
    {
    super.dispose();
    components.dispose();
    } private void button1_click(Object source, Event e){

    client.SendMsg();
    }客户端:(Client.java)
    public class Client{
    public PrintWriter out;
    public Socket socket;
    public Client(){
    try {
       InetAddress add=InetAddress.getByName(null);
       int intIndex=add.toString().indexOf("/");
       String strAdd=add.toString().substring(10);
       socket=new Socket(strAdd,Chat.PORT);
    }catch(Exception e){
       e.printStackTrace();
    }

    }
    public void SendMsg() {
    try{
       out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
       out.println("howdy");
    }catch(Exception e){
       e.printStackTrace(); }
    }
    }服务端:(Server.java)
    public class Server{
    public static  String strMess="";
    public static  ServerSocket s;
    public static  Socket socket;
    public static  BufferedReader in;
    public static  PrintWriter out;

    public Server(){
    try{
    s= new ServerSocket(Chat.PORT);
    socket=s.accept();
    }catch (Exception e){
    e.printStackTrace();
    }
    }

    public  static void listen() 
    throws com.ms.wfc.io.IOException{
    try{
    while (true){
    in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    AcceptMess();
    }
    }catch(IOException e ){
        e.printStackTrace();
    }
    }

    public static void AcceptMess()
    throws IOException{
    if (true){
               strMess=in.readLine();
       MessageBox.show(strMess+"OK!");
    }
    }
    }
      

  3.   

    To chuzhi(楚楚):
    没有Form类呀