转自:http://hi.baidu.com/lewutian/blog/item/594e9ff414de1965ddc47408.html
本文使用ServerSocket编写服务端,Socket编写客户端,或者使用NIO也行。然后使用Wireshark抓包。
所有程序在Eclipse测试通过,不过服务器和客户端要在不同的机器上运行,这是因为Wireshark不抓loopback包的问题。
我是在虚拟机中Run的,并且把虚拟机的网络连接设置成“桥”。
如下所示1-3步为程序,4步为抓包结果。
1.Server
package net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class ThreadedServer {
public static void main(String args[]) throws Exception {
int port = 52333;
ServerSocket ss = new ServerSocket(port);
System.out.println("Server started");
while (true) {
Socket s = ss.accept();
System.out.println("accept client");
ServerThread st = new ServerThread(s);
st.start();
System.out.println("start thread");
}
}
}
class ServerThread extends Thread {
DataInputStream dis;
DataOutputStream dos;
public ServerThread(Socket s) throws Exception {
InputStream is = s.getInputStream();
dis = new DataInputStream(is);
OutputStream os = s.getOutputStream();
dos = new DataOutputStream(os);
}
public void run() {
try {
while (true) {
byte[] bytes = new byte[20];
dis.read(bytes);
System.out.println("read bytes is " + new String(bytes));
dos.write(bytes);
System.out.println("write return value " + new String(bytes));
}
} catch (Exception e) {
}
}
}
2.Client
package net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class Client
{
public static void main(String args[])
throws Exception
{
int c;
Socket s = new Socket("192.158.126.136", 52333);
System.out.println("connect to 192.158.126.136:52333");
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
DataInputStream dis = new DataInputStream(in);
dos.write("maxlisa0102221122001212122121252".getBytes());
// dos.writeInt(12);
System.out.println("write maxlisa");
System.out.print("return value: ");
byte bytes[] = new byte[3];
while((c = in.read(bytes)) != -1)
{
System.out.print(new String(bytes));
}
s.close();
System.out.println("close connection");
}
}
3.or using NIO Client
package net;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;
public class NIOClient
{
public static void main(String[] args)
throws Exception
{
SocketChannel sc = SocketChannel.open();
ByteBuffer buffer = ByteBuffer.allocate(1024);
Selector selector = Selector.open();
buffer.put("maxlisa test".getBytes());
buffer.flip();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ );
sc.connect(new InetSocketAddress("192.158.126.136", 52333));
while(!sc.finishConnect());
sc.write(buffer);
System.out.println("select operate");
Thread.sleep(10000);
int sum = selector.select();
while(selector.select() > 0)
{
Thread.sleep(100);
Set
for(SelectionKey key : set)
{
int ops = key.readyOps();
if((ops & SelectionKey.OP_READ) == SelectionKey.OP_READ)
{
sc.read(buffer);
buffer.flip();
}
}
selector.selectedKeys().clear();
}
}
}
4.TCP PDU LIST
No. Time Source Destination Protocol Info
1 0.000000 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [SYN] Seq=0 Win=5840 Len=0 MSS=1460 TSV=30156603 TSER=0 WS=7
2 0.000053 192.158.126.136 192.158.125.222 TCP 52333 > 38702 [SYN, ACK] Seq=0 Ack=1 Win=64240 Len=0 MSS=1460 WS=0 TSV=0 TSER=0
3 0.000136 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [ACK] Seq=1 Ack=1 Win=5888 Len=0 TSV=30156603 TSER=0
4 0.001433 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [PSH, ACK] Seq=1 Ack=1 Win=5888 Len=32 TSV=30156604 TSER=0
5 0.001775 192.158.126.136 192.158.125.222 TCP 52333 > 38702 [PSH, ACK] Seq=1 Ack=33 Win=64208 Len=20 TSV=296107 TSER=30156604
6 0.001827 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [ACK] Seq=33 Ack=21 Win=5888 Len=0 TSV=30156605 TSER=296107
7 0.002141 192.158.126.136 192.158.125.222 TCP 52333 > 38702 [PSH, ACK] Seq=21 Ack=33 Win=64208 Len=20 TSV=296107 TSER=30156604
8 0.002191 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [ACK] Seq=33 Ack=41 Win=5888 Len=0 TSV=30156605 TSER=296107
9 7.844516 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [FIN, ACK] Seq=33 Ack=41 Win=5888 Len=0 TSV=30164412 TSER=296107
10 7.844576 192.158.126.136 192.158.125.222 TCP 52333 > 38702 [ACK] Seq=41 Ack=34 Win=64208 Len=0 TSV=296186 TSER=30164412
11 7.844894 192.158.126.136 192.158.125.222 TCP 52333 > 38702 [PSH, ACK] Seq=41 Ack=34 Win=64208 Len=20 TSV=296186 TSER=30164412
12 7.844934 192.158.125.222 192.158.126.136 TCP 38702 > 52333 [RST] Seq=34 Win=0 Len=0