java TCP client 從鍵盤讀入字串
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
inFromUser.readLine();
-------
java TCP client 從Server讀入字串
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(client.getInputStream()));
inFromServer.readLine();
-------
java TCP client 送出字串給Server
DataOutputStream outToServer =
new DataOutputStream(client.getOutputStream());
outToServer.writeBytes("string");
-------
java TCP 建立Client Socket
Socket client = new Socket("hostname",port);
--------
java TCP 建立Server Socket
ServerSocket waiting = new ServerSocket(port); //接應用WelcomeSocket
while(true){
Socket server = waiting.accept();
//其餘和 TCP Client 相同
}
---------
java UDP 建立Client Socket
DatagramSocket client = new DatagramSocket("hostname",port);
----------
java UDP 建立Server Socket
DatagramSocket server = new DatagramSocket(port);
-----------
java UDP 傳送資料
InetAddress addr = InetAddress.getByName("destination");
int port=1234;
String a = "sendData";
bytes[] b= new bytes[1024];
b = a.getBytes();
DatagramPacket outPacket = new DatagramPacket(b,b.length,addr,port);
client.send(outPacket);
-------------
java UDP 接收資料
byte b = new byte[1024];
DatagramPacket r = new DatagramPacket(b,b.length);
while(true){
server.receive(r);
int port = r.getPort();
InetAddress addr = r.getAddress();
String received = new String(r.getData());
}
- Dec 21 Tue 2010 00:00
Java Socket
全站熱搜
留言列表