谁能给个异步短连接的socket通信的客户端代码,发送到邮箱[email protected],感激不尽,确认后给分,谢谢

解决方案 »

  1.   

    各位好,我是猎头charles,现有上海的JAVA的staff级别的职位,急需7年以上做JAVA技术方面的人才,要求英语流利,JAVA技术精通,最好是不曾转管理方向,B/S架构。此职位乃上海著名全球500强外企在中国首次设立的纯技术职位,望有意者随时与我联系:
    MSN:[email protected]
    邮箱:[email protected]
      

  2.   

    import java.net.*;
    import java.io.*;public class myclient
    {
        public static void main(String args[])
        {
            
            Socket socket;
            String s = "";
            InputStream Is;
            OutputStream Os;
            BufferedReader DIS;
            PrintStream out;
            try
            {
                //向主机名为args[0]的服务器申请连接 
                //注意端口号要与服务器保持一致:4321 
                socket = new Socket(InetAddress.getLocalHost(),4000);
                
                System.out.println("client ok");
                System.out.println("************************************************");
                System.out.println("");
                
                //获得对应socket的输入/输出流 
                Is = socket.getInputStream();
                Os = socket.getOutputStream();
                
                //建立数据流 
                DIS = new BufferedReader(new InputStreamReader(Is));
                
                
                out = new PrintStream(Os);
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        System.in));
                
                while (true)
                {
                    System.out.print("你说:");
                    s = in.readLine(); //读取用户输入的字符串 
                    out.println(s); //将读取得字符串传给server 
                    if (s.trim().equals("BYE"))
                        break; //如果是"BYE",就退出 
                    else
                    {
                        System.out.println("");
                        System.out.println("请等待服务端消息...");
                        System.out.println("");
                    }
                    s = DIS.readLine(); //从服务器获得字符串 
                    System.out.println("服务器说:" + s); //打印字符串 
                    if (s.trim().equals("BYE"))
                        break; //如果是"BYE",就退出 
                        
                }
                
                //关闭连接 
                DIS.close(); //关闭数据输入流 
                out.close(); //关闭数据输出流 
                Is.close(); //关闭输入流 
                Os.close(); //关闭输出流 
                socket.close(); //关闭socket 
            }
            catch (Exception e)
            {
                System.out.println("Error:" + e);
            }
        }
    }