IPDemo
public class IPDemo {
/**
* @param args
* @throws UnknownHostException
* @throws IOException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress ip = InetAddress.getByName("www.sina.com.cn");
System.out.println(ip.getHostAddress()+":"+ip.getHostName());
}
}
UDP协议
UDPSend
public class UDPSend {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("udp 发送端 run");
DatagramSocket ds = new DatagramSocket(8888);
String text = "hello udp来了。";
byte[] buf = text.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.255"), 10000);
ds.send(dp);
ds.close();
}
}
UDPRece
public class UDPRece {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("udp 接收端 run");
DatagramSocket ds = new DatagramSocket(10000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+":"+port+":"+text);
ds.close();
}
}
UDPSend2
public class UDPSend2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("udp2 发送端 run");
DatagramSocket ds = new DatagramSocket(9999);
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line)){
break;
}
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.223"), 10001);
ds.send(dp);
}
ds.close();
}
}
UDPRece2
public class UDPRece2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("udp2 接收端 run");
DatagramSocket ds = new DatagramSocket(10001);
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
}
}
}
UDPChatTest
public class UDPChatTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002);
Send send = new Send(sendSocket);
Rece rece = new Rece(receSocket);
Thread t1 = new Thread(send);
Thread t2 = new Thread(rece);
t1.start();
t2.start();
}
}
class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
while ((line = bufr.readLine()) != null) {
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName("192.168.1.223"), 10002);
ds.send(dp);
if ("886".equals(line)) {
break;
}
}
ds.close();
} catch (IOException e) {
}
}
}
class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {
super();
this.ds = ds;
}
@Override
public void run() {
while (true) {
try {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0, dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
if(text.equals("886")){
System.out.println(ip+"....离开聊天室");
}
} catch (IOException e) {
}
}
}
}
TCP协议
TCPClient
public class TCPClient {
/**
* @param args
* @throws IOException
* @throws
*/
public static void main(String[] args) throws IOException {
System.out.println("客户端运行.......");
Socket s = new Socket("192.168.1.223",10003);
OutputStream out = s.getOutputStream();
out.write("hello tcp 来了!".getBytes());
s.close();
}
}
TCPServer
public class TCPServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("服务端开启.....");
ServerSocket ss = new ServerSocket(10003);
while (true) {
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(text);
s.close();
}
}
}
TCPClient2
public class TCPClient2 {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("客户端2 启动.......");
Socket s = new Socket("192.168.1.223", 10004);
OutputStream out = s.getOutputStream();
out.write("服务端,我来了".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
s.close();
}
}
TCPSever2
public class TCPSever2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("服务端2启动.....");
ServerSocket ss = new ServerSocket(10004);
while (true) {
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress()
+ ".....connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(text);
OutputStream out = s.getOutputStream();
out.write("客户端,我已到收到,哦耶!".getBytes());
s.close();
}
}
}