网络编程
-
TCP:用户传输协议(打电话)
-
UDP:用户数据报协议(发短信)
-
IP:InetAddress
-
本机localhost:127.0.0.1
-
-
端口:表示计算机上的一个进程(0~65535)
-
Tomcat : 8080
-
MySQL : 3306
-
Oracle : 1521
-
1.TCP实现聊天
客户端:
-
连接服务器,通过Socket
-
发送消息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDome01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道服务器的地址
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
//2.端口号
int port = 9999;
//3.创建一个socket链接
socket = new Socket(serverIP,port);
//4.发送消息 IO流
os = socket.getOutputStream();
os.write("你好!".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();
}
}
}
}
}
服务器:
-
建立服务端口,ServerSocket
-
等待用户连接,通过accept
-
接收用户的消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
//1.有一个地址
try {
serverSocket = new ServerSocket(9999);
//2.等待客户端链接
accept = serverSocket.accept();
//3.读取客户端的消息
is = accept.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException 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 (accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.多线程实时聊天
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket();
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int prot;
private String msgFrom;
public TalkReceive(int prot,String msgFrom) {
this.prot = prot;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(prot);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
//准备接收包裹
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);//阻塞式接收包裹
//读取数据
byte[] data = packet.getData();
String recevieData = new String(data, 0, data.length);
System.out.println(msgFrom + ":" + recevieData);
//断开链接
if (recevieData.equals("bye")){
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
3.url下载网络资源
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDown {
public static void main(String[] args) throws Exception {
URL url = new URL("https://nx01-sycdn.kuwo.cn/af16361673a9efd43d4bd6f51c885303/6143fade/resource/n2/36/44/1478416719.mp3");
//连接到这个资源
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("稻香.mp3");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes)) != -1){
fos.write(bytes,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}

被折叠的 条评论
为什么被折叠?



