Java网络编程
TCP实现聊天
客户端
//客户端
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientdemo01 {
private static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
// 创建socket连接
socket = new Socket(serverIP, port);
// 发送消息IO流
os = socket.getOutputStream();
os.write("hello!".getBytes());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}
服务器端
//服务器端
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.Port;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayInputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayInputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buff)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}
初识Tomcat
C/S架构和B/S架构
服务器端
- 自定义 S
- Tomcat服务器 S
客户端
- 自定义 C
- 浏览器 B
UDP
发消息,不用连接,需要知道对方的地址
服务器端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpCLientDemo01{
public static void main(String[] args) {
//建立一个socket
DatagramSocket socket = new DatagramSocket();
//建个包
String msg = "你好,服务器!";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),msg.length(),localhost,port);
//发送包
socket.send(packet);
//关闭流
socket.close();
}
}
客户端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class udpserver {
public static void main(String[] args) {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length, socket);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
多线程实现UDP聊天
思路:通过在一个类里创建两个线程,一个用来发送消息,一个用来接收消息,实现了全双工的聊天程序。
实例中老师和学生两个类分别创建发送和接收两个线程来实现聊天。
具体实现如下:
老师类
package com.queryor.lesson03.chat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
学生类
package com.queryor.lesson03.chat;
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
发送消息类
package com.queryor.lesson03.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private String toIp;
private int fromPort;
private int toPort;
public TalkSend(int fromPort, String toIp, int toPort) {
this.toIp = toIp;
this.fromPort = fromPort;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}catch (Exception 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 (Exception e){
e.printStackTrace();
}
}
socket.close();
System.out.println("send over");
}
}
接收消息类
package com.queryor.lesson03.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(this.port);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data,0,data.length,"utf-8");
System.out.println(msgFrom+":"+receiveData);
System.out.println(receiveData);
if(receiveData.trim().equals("bye")){
System.out.println(receiveData);
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
System.out.println("receive over");
}
}