static final String HOST = "***.***.***.231";
static final int PORT = 6000;
socket = new Socket(HOST, PORT);
socket初始化结束。。当然需要在头文件声明package
import java.net.Socket;
这比C里面初始化socket容易的多。因为高度封装化嘛,有利有弊。
然后用socket呢
接收数据
int PacketSize = 1024;
byte[] recvbuf = new byte[PacketSize];
InputStream is = socket.getInputStream();
countRead = is.read(recvbuf_count, 0, 10);
发送数据
byte[] sendbuf = new byte[PacketSize];
OutputStream send = socket.getOutputStream();
send.write(sendbuf,0,10);
write(byte[] b, int off, int len)
Writes len
bytes from the specified byte array starting at offset off
to this output stream.