java SE基础(TCP Socket通信),sesocket
分享于 点击 11223 次 点评:249
java SE基础(TCP Socket通信),sesocket
1。socket对象的两种初始化方法*/
/* 方法一 */Socket socket = new Socket() //创建无参socket对象
SocketAddress socketaddress = new InetSocketAddress(String hostname , int port)//创建SocketAddress对象,实现 IP 套接字地址(IP 地址 + 端口号)或(主机名 + 端口
socket.connect( socketaddress ) //将此套接字连接到服务器
/* 方法二 */
Socket socket = new Socket(String hostname , int port)//创建对象的同时进行初始化
2。socket的流操作*/
public InputStream getInputStream(){} //返回套接字的网络输入流public OutputStream getOutputStream(){} //返回套接字的网络输出流
3。ServerSocket的两种初始化方法*/
/* 方法一 */ServerSocket serversocket = new ServerSocket() //创建无参对象
SocketAddress socketaddress = new InetSocketAddress(int port) //创建SocketAddress对象
serversocket.bind(socketaddress) //绑定到特定地址
/* 方法二 */
ServerSocket serversocket = new ServerSocket(int port)
4。ServerSocket的操作方法*/
public Socket accept(){} //侦听并接受到此套接字的连接,返回Socket对象5。TCP socket通信过程
(1)服务器端创建一个ServerSocket对象,指定服务器端口号,ServerSocket对象调用accept()方法,等待客户端的连接请求;(2)客户端创建一个Socket对象,指定服务器的主机和端口号,向服务器发送连接请求;
(3)服务器端收到客户端的连接请求,建立一条TCP连接,由accept()方法返回一个Socket对象,与客户端的Socket对象进行通信;
(4)服务器端和客户端的socket对象各自调用socket的流操作方法,向对方发送数据;
(5)通信结束后,先后调用close()方法,关闭socket和serversocket */
6。例如:*/
/* 服务器端 */
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServerSocket {
public static final int SERVER_PORT = 48123; //定义服务器端口号
//定义接收文件的方法
public static void receiveFile(Socket socket) throws IOException {
String desFile = "java.java"; //待写入文件
DataInputStream dis = new DataInputStream(socket.getInputStream()); //定义网络输入流,接收来自客户端的数据
FileOutputStream fos = new FileOutputStream(desFile); //定义文件输出流,将接收到的数据写到本地文件
byte[] buffer = new byte[512]; //定义输入缓冲区
int length = 0; //定义缓冲区有效长度
System.out.println("开始接收数据...");
while( (length = dis.read(buffer)) != -1 ){ //读数据
fos.write(buffer , 0 , length);
fos.flush();
}
System.out.println("完成接收:"+desFile);
fos.close();
dis.close();
socket.close();
}
public static void main(String[] args) throws IOException {
ServerSocket serversocket = new ServerSocket(); //创建ServerSocket对象
serversocket.bind(new InetSocketAddress(SERVER_PORT));
System.out.println("开始监听...");
Socket socket = serversocket.accept();
System.out.println("有链接");
receiveFile(socket);
serversocket.close();
}
}
/* 客户端 */
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class MyClient {
public static final int SERVER_PORT = 48123; //定义服务器端口号
public static void main(String[] args) throws IOException {
/* Socket socket = new Socket(); //建立套接字连接
socket.connect(new InetSocketAddress("127.0.0.1", SERVER_PORT));*/
Socket socket = new Socket("localhost", SERVER_PORT);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //网络输出流
String srcFile = "java提炼.java";
FileInputStream fis = new FileInputStream(srcFile); //文件输入流
byte[] buffer = new byte[512]; //缓冲区
int length = 0; //缓冲区有效长度
System.out.println("开始读取");
while( (length = fis.read(buffer)) != -1){ //读取文件,并写入网络输出流
dos.write(buffer, 0, length);
dos.flush();
}
System.out.println("写入完毕");
dos.close();
fis.close();
socket.close();
}
}
相关文章
- 暂无相关文章
用户点评