10.Java----socket编程
2019-07-15 本文已影响0人
_麻辣香锅不要辣
1.TCP
1.客户端:
1.1:创建一个socket对象,指定ip和端口
1.2: 通过socket.getOutputStream()获取输出流,来发送数据
1.3:如果服务器有返回信息,则通过socket.getInputStream(),来获取信息。在此之前,客户端在发送完数据之后,需要通过socket.shutdownOutput()将输出流关闭,否则服务器端会一直等待客户端发送消息
2.服务端:
2.1:创建一个serverSocket
2.2:通过serverSocket.accept()来获取客户端的socket
2.3:通过客户端的socket来接收、发送数据
public class SocketTest {
@Test
public void client(){
try {
Client yang = new Client("yang");
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
yang.sentMsg(inetAddress,8000,"你好啊");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
@Test
public void server(){
Server server = new Server(8000);
server.getMsg();
}
}
class Client{
private String name;
private Socket socket;
public Client(String name) {
this.name = name;
this.socket = null;
}
public void sentMsg(InetAddress inetAddress,int port,String msg){
OutputStream os = null;
try {
//创建socket对象,指定ip和端口
socket = new Socket(inetAddress,port);
//获取输出流,用于输出数据
os = socket.getOutputStream();
os.write(msg.getBytes());
System.out.println("发送成功");
System.out.println("Client1:"+socket.isOutputShutdown());
socket.shutdownOutput(); //关闭socket的输出,否则服务器端会一直等待着接受数据
System.out.println("Client2:"+socket.isOutputShutdown());
//获取服务端返回的信息
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while((len = inputStream.read(bytes)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流和socket
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
class Server{
private ServerSocket serverSocket;
public Server(int port) {
try {
this.serverSocket = new ServerSocket(8000);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getMsg(){
Socket accept = null;
ByteArrayOutputStream baos = null;
OutputStream os = null;
try {
//获取客户端的socket
accept = serverSocket.accept();
//获取输入流
InputStream is = accept.getInputStream();
//读取输入流
byte[] bytes = new byte[1024];
int len;
baos = new ByteArrayOutputStream();
System.out.println("Server1:"+accept.isOutputShutdown());
while((len = is.read(bytes)) != -1){
System.out.println("Server3:"+accept.isOutputShutdown());
baos.write(bytes,0,len);
}
System.out.println("Server2:"+accept.isOutputShutdown());
System.out.println("收到"+accept.getInetAddress()+"的消息");
System.out.println(baos.toString());
//返回给客户端消息
System.out.println("开始回复");
os = accept.getOutputStream();
os.write("我已经收到了".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(baos != null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.UDP
1.客户端:
1.1:创建一个socket对象
1.2: 创建一个packet对象,存放数据,ip和端口
1.3:通过socket.send()发送数据包
2.服务端:
2.1:创建一个socket对象,指定端口
2.2:通过socket.receive(packet)来接收数据包并保存在packet中
public class SocketTest2 {
@Test
public void sender(){
try {
//创建一个socket
DatagramSocket socket = new DatagramSocket();
//创建一个packet,存放数据
byte[] bytes = "你好啊".getBytes();
DatagramPacket packet = new DatagramPacket(bytes,bytes.length,
InetAddress.getByName("127.0.0.1"),
8000);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void reciver(){
try {
DatagramSocket socket = new DatagramSocket(8000);
byte[] bytes = new byte[100];
DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
}
}
3.URL
3.1:创建URL对象
3.2:创建urlConnection对象
3.3:连接
3.4:通过urlConnection对象获取数据
public class URLTest {
@Test
public void test1(){
try {
URL url = new URL("https://www.baidu.com");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("./baidu.html");
byte[] bytes = new byte[1024];
int len;
while((len = is.read(bytes)) != -1){
fos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}