欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java网络编程--利用TCP协议发送接收数据

程序员文章站 2024-03-23 10:06:16
...

使用TCP协议发送数据的步骤:

  • 创建Socket对象(创建连接)
  • 获取输出流对象
  • 发送数据
  • 释放资源
package _net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class _TCP_send {
public static void main(String[] args) throws UnknownHostException, IOException {
	//创建Socket对象
	//通过Socket(InetAddress address,int port)构造方法 创建一个Socket对象并传入目标IP和设置的端口号
	Socket socket = new Socket(InetAddress.getByName(String 目标IP),9999);
	//获取输出流对象

	OutputStream oStream = socket.getOutputStream();
	//发送数据
	String string = "hello world";
	oStream.write(string.getBytes());
	//释放资源
	socket.close();
	oStream.close();
}
}

使用TCP协议接收数据的步骤:

  • 创建接收端Socket对象 明确端口号
  • 监听(阻塞) 等待客户端连接
  • 获取输入流对象
  • 接收数据(输入流) --发送数据用的是输出流
  • 输出数据
  • 释放资源
package _net;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;



/*
 * ServerSocket:接收端 服务端Socket:
 * ServerSocket(int port):明确端口号
 * 
 */
public class _TCP_receive {
public static void main(String[] args) throws IOException {
	ServerSocket serverSocket = new ServerSocket(9999);
	//监听(阻塞) --返回值应该是一个具体的Socket对象来获取输入流方法
	Socket socket =  serverSocket.accept();//当有一个输出端连接到接收端,accept方法就可以获取到连接到接收端的Socket对象
	//获取输入流对象
	InputStream inputStream = socket.getInputStream();
	//接收数据
	byte[] byt = new byte[1024];
	int len;
	len = inputStream.read(byt);//用len存储inputStream输入对象读取到的字节个数
	//输出接收到的数据
	InetAddress inetAddress = socket.getInetAddress();
	inetAddress.getHostName();
	System.out.println("send-->"+inetAddress);
	System.out.println(new String(byt,0,len));
	//释放资源
	serverSocket.close();
	socket.close();
	inputStream.close();
	
}
}

TCP协议发送接收数据:
在服务器接收数据并将所有小写字母转换为大写字母然后返回客户端

package _net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class _TCP_send_toUpString {
public static void main(String[] args) throws UnknownHostException, IOException {
	Socket socket = new Socket(InetAddress.getByName(String 服务端IP),7777);
	
	OutputStream outputStream = socket.getOutputStream();
	
	String string = "qwertyuiop";
	
	outputStream.write(string.getBytes());
	
	socket.close();
	outputStream.close();
	
	ServerSocket serverSocket = new ServerSocket(6666);
	Socket socket_receiveSocket = serverSocket.accept();
	InputStream inputStream = socket_receiveSocket.getInputStream();
	byte[] byt = new byte[1024];
 	int Length = inputStream.read(byt);
 	InetAddress inetAddress = socket_receiveSocket.getInetAddress();
 	inetAddress.getHostName();
 	System.out.println("send-->"+inetAddress);
 	System.out.println(new String(byt,0,Length));
	
}
}

package _net;

/**
 * 接收发送端发来的字符串 全变大写并返回
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class _TCP_receive_toUpString {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(7777);
		Socket socket = serverSocket.accept();
		
		InputStream inputStream = socket.getInputStream();
		byte[] byt = new byte[1024];
		int Length = inputStream.read(byt);
		
		InetAddress inetAddress = socket.getInetAddress();
		String name = inetAddress.getHostName();
		System.out.println("send-->"+inetAddress);
		String string = new String (byt,0,Length);
		System.out.println(string);
		
		String newstring = string.toUpperCase();
		//System.out.println(newstring);
		//将变大写的字符串发送回去
		Socket socket_sendSocket =  new Socket(InetAddress.getByName(String 客户端IP),6666);
		
		OutputStream outputStream = socket_sendSocket.getOutputStream();
		
		outputStream.write(newstring.getBytes());
		
		serverSocket.close();
		socket.close();
		socket_sendSocket.close();
		outputStream.close();
		inputStream.close();

	}
}

相关标签: socket java