tcp | udp | |
---|---|---|
服务端 | ServerSocket | DatagramSocket |
客户端 | Socket | DatagramSocket |
全是示例
tcp客户端示例
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.准备要链接的服务端的信息
InetAddress addresses = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建套接字对象链接
socket = new Socket(addresses, port);
//3.发送消息
os = socket.getOutputStream();
os.write("你好,欢迎学习狂神说Java".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
tcp服务端示例
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpSeverDemo01 {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.ip为服务器本机地址。 创建一个服务 设定端口号即可
server = new ServerSocket(9999);
while(true) {
//2.等待客户端链接
socket = server.accept();
//3.读取客户端发送的消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while((len=is.read(bytes))!= -1) {
baos.write(bytes, 0, len);
}
System.out.println(baos.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
udp模拟聊天示例:
发消息的类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader br = null;
private String toIP;
private int toPort;
public TalkSend(String toIP, int toPort) {
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket();//准备链接
br = new BufferedReader(new InputStreamReader(System.in));//接收控制台输入
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
//准备数据包
byte[] data = new byte[0];
try {
data = br.readLine().getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, InetAddress.getByName(this.toIP), this.toPort);
//发送包
socket.send(packet);
} catch (Exception e) {
e.printStackTrace();
}
String msg = new String(data,0,data.length);
if(msg.equals("bye")) {
break;
}
}
//关闭链接
try {
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
收消息的类
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkRecieve implements Runnable{
DatagramSocket socket = null;
private String name;
public TalkRecieve(int fromPort, String name) {
try {
this.name = name;
this.socket = new DatagramSocket(fromPort);//准备链接
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while(true) {
//准备容器
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收
String s = new String(packet.getData(), 0, packet.getData().length);
System.out.println(name + ":" +s);
if(s.equals("bye")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭资源
socket.close();
}
}
模拟启动一个老师
public class TestTeacher {
public static void main(String[] args) {
new Thread(new TalkSend("127.0.0.1", 8888)).start();//发送消息
new Thread(new TalkRecieve(7777, "学生说")).start();//接收消息
}
}
模拟启动一个学生
public class TestStudent {
public static void main(String[] args) {
new Thread(new TalkSend("127.0.0.1", 7777)).start();//发送消息
new Thread(new TalkRecieve(8888, "老师说")).start();//接收消息
}
}
url类的测试使用
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestUrl {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/kuangshen/haha.txt");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream("xxx.txt");
byte[] bytes = new byte[1024];
int len;
while((len = is.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fos.close();
is.close();
connection.disconnect();
}
}