今天主要讲了TCP的收发
TCP发端步骤
①socket
②connect
③send
④close
TCP收端步骤
①socket
②bind
③listen
④accept
⑤recv
⑥clise
其函数主要有
connect
c
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
功能:
发送链接请求
参数:
sockfd:套接字文件描述符
addr:目的地址存放空间首地址
addrlen:IP地址的大小
返回值:
成功返回0
失败返回-1
send
c
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
功能:
发送数据
参数:
sockfd:文件描述符
buf:发送数据空间首地址
len:发送数据的长度
flags:属性默认为0
返回值:
成功返回实际发送字节数
失败返回-1
recv
c
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
功能:
接收数据
参数:
sockfd:套接字文件描述符
buf:存放数据空间首地址
len:最大接收数据的长度
flags:属性默认为0
返回值:
成功返回实际接收字节数
失败返回-1
如果对方退出,返回0
listen
c
int listen(int sockfd, int backlog);
功能:
监听客户端发送的连接请求
该函数不会阻塞
参数:
sockfd:套接字文件描述符
backlog:允许等待的尚未被处理的三次握手请求的最大个数
返回值:
成功返回0
失败返回-1
accept
c
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
功能:
处理等待连接队列中的第一个连接请求
该函数具有阻塞功能(如果没有人发送链接请求,会阻塞等待)
参数:
socket:套接字文件描述符
address:存放IP地址的空间首地址
addrlen:存放IP地址大小空间首地址
返回值:
成功返回一个新的文件描述符
失败返回-1
eg:利用TCP实现跨主机的文件发送
send端
c
#include"head.h"
int main(void)
{
int sockfd = 0;
FILE *fp = 0;
int ret = 0;
ssize_t nsize = 0;
ssize_t nret = 0;
char tmpbuff[4096] = {0};
struct sockaddr_in srcaddr;
char filename[256] = {"a.txt"};
gets(filename);
srcaddr.sin_family = AF_INET;
srcaddr.sin_port = htons(50000);
srcaddr.sin_addr.s_addr = inet_addr("192.168.1.152");
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockfd)
{
perror("fail to socket");
return -1;
}
ret = connect(sockfd,(struct sockaddr *)&srcaddr,sizeof(srcaddr));
if(-1 == ret)
{
perror("fail to connect");
return -1;
}
nsize = send(sockfd,filename,strlen(filename),0);
if(-1 == nsize)
{
perror("fail to send");
return -1;
}
fp = fopen(filename, "rb");
if (fp == NULL)
{
perror("fail to fopen");
return -1;
}
while(1)
{
memset(tmpbuff,0,sizeof(tmpbuff));
sleep(1);
nsize = fread(tmpbuff, sizeof(char), 4096, fp);
if(0 >= nsize)
{
break;
}
send(sockfd, tmpbuff,nsize, 0);
}
close(sockfd);
fclose(fp);
return 0;
}
recv端
c
#include"head.h"
int main(void)
{
FILE *file = NULL;
ssize_t nsize = 0;
ssize_t nret = 0;
char name[20] = {0};
char tmpbuff[4096] = {0};
int sockfd = 0;
int recfd = 0;
int ret = 0;
struct sockaddr_in recaddr;
recaddr.sin_family = AF_INET;
recaddr.sin_port = htons(50000);
recaddr.sin_addr.s_addr = INADDR_ANY;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockfd)
{
perror("fail to socket");
return -1;
}
ret = bind(sockfd,(struct sockaddr *)&recaddr,sizeof(recaddr));
if(-1 == ret)
{
perror("fail to bind");
return -1;
}
ret = listen(sockfd,10);
if(-1 == ret)
{
perror("fail to listen");
return -1;
}
recfd = accept(sockfd,NULL,NULL);
if(-1 == recfd)
{
perror("fail to accept");
return -1;
}
nret = recv(recfd,name,sizeof(name),0);
if(-1 == nret)
{
perror("fail to recv");
return -1;
}
file = fopen(name,"wb");
while ((nsize = recv(recfd, tmpbuff, sizeof(tmpbuff), 0)) > 0)
{
if (fwrite(tmpbuff, sizeof(char), nsize, file) < nsize)
{
perror("fail to write file");
fclose(file);
close(recfd);
close(sockfd);
return -1;
}
}
close(recfd);
fclose(file);
close(sockfd);
return 0;
}
结果:
以上就是今天内容!