JAVA TCP SOCKET

TcpConnect.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package o1;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpConnect {
    // 1-b 
    // 用于操作数据的流 和 主要的
    /**
     * 发给服务器
     */
    private OutputStreamWriter outputStreamWriter;
    /**
     * 返回给客户端 服务器对客户端的回复
     */
    private InputStreamReader inputStreamReader;
    private Socket socket = null;
    
    // IP地址和端口port
    private String ip;
    private Integer port;
    private final Integer TIMEOUT = 1000; // ms
    // 1-e
        
    public TcpConnect(String ip, Integer port) throws UnknownHostException, IOException, Exception {
        this.ip = ip;
        this.port = port;
                
        InetAddress theAddress = InetAddress.getByName(ip);
        
        InetSocketAddress inetSocketAddress = new InetSocketAddress(theAddress, port);
//        socket = new Socket(theAddress, port);
        socket = new Socket();
        socket.connect(inetSocketAddress, TIMEOUT);
                
        OutputStream output = socket.getOutputStream();
        outputStreamWriter = new OutputStreamWriter(output);

        InputStream in = socket.getInputStream();
        BufferedInputStream buStream = new BufferedInputStream(in);
        inputStreamReader = new InputStreamReader(buStream);
    }
    
    // gets, sets
}

引用
Cobble.Ge 于 合肥 公司 20120524

你可能感兴趣的:(java,socket,tcp,tcpconnect)