Socket
1. url.openStream获取链接的输入流(字节流)
2. tcp
1) 服务端ServerSocket,创建时连带端口号,而客户端Socket,包括ip,端口号
2) 服务器端通过serverSocket.accept(),进行监听,返回的是Socket类,可以通过这个类,用InetAddress获取访问的ip等等
3) 通过ServerSocket,Socket的getInputStream,getOutStream获取字节流
4) 多线程连接,把服务器的读取数据,返回数据写在run方法中,主线程每次accept()后,创建多一个线程。
3. Udp
1) 服务器端和客户端都是使用 DatagramSocket,但服务器端直接创建接口,客户端需要设置传过的数据,ip,端口(创建时完成)。数据是以字节数组的形式。
2) 数据报DatagramPacket,设置字节数组,长度
3) datagramSocket.receive(packet);接收数据报 socket.send(packet);发送数据报
4) 收到的数据转成String类 String reply=new String(字节数组,0,数据报.getLength());
TCP实例
服务器端代码
4. package test;
5.
6. import java.io.BufferedReader;
7. import java.io.IOException;
8. import java.io.InputStream;
9. import java.io.InputStreamReader;
10. import java.io.OutputStream;
11. import java.io.PrintWriter;
12. import java.net.InetAddress;
13. import java.net.ServerSocket;
14. import java.net.Socket;
15.
16. public class test1 {
17. public static void main(String[] args) throws IOException {
18. ServerSocketserverSocket=new ServerSocket(8888);
19. System.out.println("服务器端开启,准备监听8888端口...");
20. Socketsocket=null;
21. int count=0;
22. while(true){
23. socket=serverSocket.accept();
24.
25. test3serverThread=new test3(socket);
26. serverThread.start();
27. count++;
28. System.out.println();
29. System.out.println("客戶端數量:"+count);
30. InetAddressinetAddress=socket.getInetAddress();
31. System.out.println("当前客户端的ip:"+inetAddress.getHostAddress());
32. }
33.
34. }
35. }
客户端代码
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class test2 {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket=new Socket("localhost", 8888);
OutputStreamos=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("用户名:admin;密码:123");
pw.flush();
socket.shutdownOutput();
InputStreaminputStream=socket.getInputStream();
BufferedReader br=new BufferedReader(newInputStreamReader(inputStream));
String n=null;
System.out.print("这是客户端,服务器端传来数据:");
while((n=br.readLine())!=null){
System.out.print(n);
}
br.close();
inputStream.close();
pw. close();
os.close();
socket.close();
}
}
线程,为多线程做准备
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class test3 extendsThread{
Socket socket=null;
publictest3(Socket socket){
this.socket=socket;
}
public void run() {
InputStream inputStream= null;
InputStreamReaderinputStreamReader= null;
BufferedReader bufferedReader= null;
OutputStream outputStream= null;
PrintWriter pw= null;
try {
inputStream=socket.getInputStream();
inputStreamReader=new InputStreamReader(inputStream);
bufferedReader=new BufferedReader(inputStreamReader);
String n=null;
System.out.print("这是服务器端,客户端传来数据:");
while((n=bufferedReader.readLine())!=null){
System.out.print(n);
}
socket.shutdownInput();
outputStream=socket.getOutputStream();
pw=new PrintWriter(outputStream);
pw.write("欢迎你!");
pw.flush();
pw.close();
outputStream.close();
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
socket.close();
} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
UDP实例
服务器端代码
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket=new DatagramSocket(8800);
//创建数据报,用于接收客户端发送的数据
byte[]data=new byte[1024];
DatagramPacket packet=new DatagramPacket(data, data. length);
//接收数据
System.out.println("服务器端已经开启,等待客户端访问...");
datagramSocket.receive(packet);//此方法在接收到数据报之前会一直阻塞
//读取数据
String info=new String(data,0,packet.getLength());
System.out.println("服务器端,客户端传过来数据:"+info);
InetAddressaddress=packet.getAddress();
intport=packet.getPort();
byte[]data2="欢迎你!".getBytes();
DatagramPacket packet2=new DatagramPacket(data2, data2.length,address,port);
datagramSocket.send(packet2);
}
}
客户端代码
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UDPClient {
public static void main(String[] args) throws IOException {
InetAddress address=InetAddress.getByName("localhost");
intport=8800;
byte[]data="用户名:admin,密码:123".getBytes();
DatagramPacket packet=new DatagramPacket(data, data.length,address,port);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
byte[]data2=new byte[1024];
DatagramPacket packet2=new DatagramPacket(data2, data2.length);
socket.receive(packet2);
String reply=new String(data2,0,packet2.getLength());
System.out.println("客户端,服务器传过来数据:"+reply);
socket.close();
}
}