TCP例子:
Server端:
import java.net.*;
import java.io.*;
public class TCPServer{
public static void main(String args[]) throws Exception{
ServerSocket ss =new ServerSocket(7777); //build a 7777 port
while(true){
Socket s = ss.accept(); //connect with client on 7777 port
DataInputStream dis = new DataInputStream(s.getIntputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
System.out.println("A Client connect!");
}
}
}
Client端:
import java.net.*;
import java.io.*;
public class TCPClient{
public static void main(String argsp[]) throws Exception{
Socket s = new Socket("127.0.0.1",7777); //build a 7777 port
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server");
dos.flush();
dos.close();
s.close();
}
}
UDP例子1:
Server端:
import java.net.*;
import java.io.*;
public class TestUDPServer{
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024]; //room to accept data from client
DatagramPacket dp = new DatagramPacket(buf,buf.length); //accept data from client,but the data is stored in buf
DatagramSocket ds = new DatagramSocket(5678); //define the socket port 5678
while(true){
ds.receive(dp); //socket accept data
System.out.println(new String(buf,0,dp.getLength())); //dp.getLength means the truly length of dp,not the length of buf
}
}
}
Client端:
import java.net.*;
import java.io.*;
public class TestUDPClient{
public static void main(String args[]) throws Exception{
byte[] buf = (new String("Hello")).getBytes(); //the data will sent
DatagramPacket dp = new DatagramPacket(buf,buf.length,
new InetSocketAddress("127.0.0.1",5678)); //throws the data to packet to send, the address is where the data send
DatagramSocket ds = new DatagramSocket(9999); // it means the client is on port 9999, and it send data to port 5678
ds.send(dp); //send the packet
ds.close(); //close the socket
}
}
UDP例子2,输入输出long类型的数:
Server端:
import java.net.*;
import java.io.*;
public class TestUDPServer1{
public static void main(String args[])throws Exception{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
DatagramSocket ds = new DatagramSocket(6666);
while(true){
ds.receive(dp);
//System.out.println(new String(buf,0,dp.getLength()));
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readLong());
}
}
}
Client端:
import java.net.*;
import java.io.*;
public class TestUDPClient1{
public static void main(String args[]) throws Exception{
long n = 10000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(n); //write to buf
byte buf[] = baos.toByteArray(); //put n to buf, and it must be in byte way, the toByteArray return a byteArray
//System.out.println(buf.length);
DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",6666));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}