IP
//得到ip的两种方式
InetAddress ip = InetAddress.getByName("燕子-M4A27I2");
InetAddress ip = InetAddress.getByName("192.168.169.1");
通信协议(UDP和TCP)
UDP协议
直接发送消息,不管对方是否接收。
发送快,但不稳定
单向发送信息
发送端代码
package com.yanzi.chapter2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class Demo2_UDP_Send {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
while(true) {
String str = s.nextLine();
if(str.equals("end")) break;
DatagramSocket ds = new DatagramSocket();
byte[] buf=str.getBytes();
int length = buf.length;
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 7878;
DatagramPacket dp = new DatagramPacket(buf,length,address,port);
ds.send(dp);
}
//ds.close();
}
}
接收端代码
package com.yanzi.chapter2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Demo03_UDP_Receive {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(7878);
while(true) {
byte[] buf = new byte[1024];
int length=buf.length;
DatagramPacket dp = new DatagramPacket(buf, length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
//接收端的一些信息
System.out.println("发送端的IP地址:"+dp.getAddress()+":"+str);
}
//ds.close();
}
}
双向发送信息
发送线程
package com.yanzi.chapter2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class SendThread extends Thread {
private int port;
public SendThread(int port) {
this.port = port;
}
@Override
public void run() {
DatagramSocket ds=null;
try {
Scanner s = new Scanner(System.in);
ds=new DatagramSocket();
while(true) {
String str = s.nextLine();
if(str.equals("end")) break;
byte[] buf=str.getBytes();
int length = buf.length;
InetAddress address = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(buf,length,address,port);
ds.send(dp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(ds!=null)
ds.close();
}
}
}
接收线程
package com.yanzi.chapter2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveThread extends Thread {
private int port;
public ReceiveThread(int port) {
this.port = port;
}
@Override
public void run() {
DatagramSocket ds =null;
try {
ds=new DatagramSocket(port);
while(true) {
byte[] buf = new byte[1024];
int length=buf.length;
DatagramPacket dp = new DatagramPacket(buf, length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
//接收端的一些信息
System.out.println("发送端的IP地址:"+dp.getAddress()+":"+str);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(ds!=null)
ds.close();
}
}
}
用户1和用户2
package com.yanzi.chapter2;
public class Demo04_Chat_User01 {
public static void main(String[] args) {
//给用户1一个发送端
new SendThread(7878).start();
//给用户1一个接收端
new ReceiveThread(7879).start();
}
}
package com.yanzi.chapter2;
public class Demo05_Chat_User02 {
public static void main(String[] args) {
//给用户2一个接收端
new ReceiveThread(7878).start();
//给用户2一个发送端
new SendThread(7879).start();
}
}
TCP协议
使用三次握手建立连接,再进行通信。
比较慢,但是比较可靠。
单向发送数据
发送端
package com.yanzi.chapter2;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Demo06_TCP_Send {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1",7878);//指定ip port建立连接
OutputStream output = s.getOutputStream();
Scanner scanner = new Scanner(System.in);
while(true) {
String str = scanner.nextLine();
if(str.equals("end"))break;
output.write(str.getBytes());
}
s.close();
}
}
接收端
package com.yanzi.chapter2;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo07_TCP_Receive {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(7878);
Socket client = ss.accept();//接收,建立跟发送端的连接
InputStream input = client.getInputStream();
byte[] buf = new byte[1024];
int length = -1;
//发送端调用close(),才会返回-1
while((length=input.read(buf))!=-1) {
System.out.println(new String(buf,0,length));
}
client.close();
ss.close();
}
}
TCP双向聊天
package com.yanzi.chapter2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Demo06_TCP_Send {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1",7878);//指定ip port建立连接
InputStream input = s.getInputStream();
OutputStream output = s.getOutputStream();
//创建一个线程用来输入
Thread t = new Thread() {
@Override
public void run() {
int length=-1;
byte[] buf = new byte[1024];
try {
while(true){
if(s.isClosed())break;
length=input.read(buf);
if(length==-1)break;
System.out.println(new String(buf,0,length));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.setDaemon(true);
t.start();
Scanner scanner = new Scanner(System.in);
while(true) {
String str = scanner.nextLine();
if(str.equals("end"))break;
output.write(str.getBytes());
}
s.close();
}
}
package com.yanzi.chapter2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Demo07_TCP_Receive {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(7878);
Socket client = ss.accept();//接收,建立跟发送端的连接
InputStream input = client.getInputStream();
OutputStream output = client.getOutputStream();
new Thread() {
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
while(true) {
if(client.isClosed())break;
String str = scanner.nextLine();
if(str.equals("end")) break;
try {
output.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
byte[] buf = new byte[1024];
int length = -1;
while((length=input.read(buf))!=-1) {
System.out.println(new String(buf,0,length));
}
client.close();
ss.close();
}
}