我想知道在Socket编程时,要怎么样从客户端写入一组字符串到流中,然后在服务端接收呀!如要发送,用户名,密码两个字符串过去,谁能不能告诉我这个IO要怎么写呀?给点源码谢谢!

解决方案 »

  1.   

    /*
     * Created on 2005-9-23
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.softbrain.wangzl.net.socketdata;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.SocketAddress;
    import java.net.SocketTimeoutException;
    import java.net.UnknownHostException;/**
     * @author wangzl
     *
     * @MSN [email protected]
     * 
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class SocketCilent {    public static void main(String[] args) {
            try {
                InetAddress addr = InetAddress.getByName("localhost");
                int port = 2000;
                SocketAddress sockaddr = new InetSocketAddress(addr, port);
            
                // Create an unbound socket
                Socket sock = new Socket();
            
                // This method will block no more than timeoutMs.
                // If the timeout occurs, SocketTimeoutException is thrown.
                int timeoutMs = 2000;   // 2 seconds
                sock.connect(sockaddr, timeoutMs);
                
                byte[] array = new byte[1024];
                BufferedInputStream bis = new BufferedInputStream(System.in);
                BufferedOutputStream bos = new BufferedOutputStream(sock.getOutputStream());
                
                while (bis.read(array)!=-1) {
                    bos.write(array, 0, array.length);
                }
                
            } catch (UnknownHostException e) {
            } catch (SocketTimeoutException e) {
            } catch (IOException e) {
            }
            
            finally {
                
            }
        }
    }
      

  2.   

    /*
     * Created on 2005-9-23
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package com.softbrain.wangzl.net.socketdata;import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;/**
     * @author wangzl
     *
     * @MSN [email protected]
     * 
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class SocketServer {    public static void main(String[] args) {
            try {
                int port = 2000;
                ServerSocket srv = new ServerSocket(port);
            
                // Wait for connection from client.
                Socket socket = srv.accept();
                
                BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                byte[] array = new byte[1024];
                while (bis.read(array)!=-1) {
                    System.out.println(new String(array, "GBK"));
                }
            } catch (IOException e) {
            }    }
    }
      

  3.   

    Socket编程IO怎么写呀,从界面接收两个字符串,然后发送到服务端,服务端用两个变量接收。 
    那位帮帮忙呀!!!
      

  4.   

    在两个字符串之间加一个特殊的分隔字符,服务器接收过来一后再用StringTokenizer把字符串转换
      

  5.   

    那你把String对象写到流 在服务器端使用readObject()方法就可以了