#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <stdarg.h>
#include <time.h>
#include <sys/stat.h>
int PtTcpSnd(int sockfd, char *buffer, int length)
{
int len;
int totalcnt;
totalcnt = 0;
printf("Now Send Socket[%d]...",sockfd);
while(totalcnt < length)
{
len = write(sockfd, buffer + totalcnt, length - totalcnt);
printf("Send Socket len[%d]",len);
if (len < 0)
{
if (errno == EINTR)
{
len = 0;
break;
}
else
return(-1);
}
else if (len == 0)
break;
totalcnt = totalcnt + len;
}
return(totalcnt);
}
int main()
{
int cfd;
struct sockaddr_in s_add;
unsigned short portnum=12306;
printf("Hello,welcome to client !\r\n");
cfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == cfd)
{
printf("socket fail ! \r\n");
return -1;
}
printf("socket ok !\r\n");
bzero(&s_add,sizeof(struct sockaddr_in));
s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr= inet_addr("127.0.0.1");
s_add.sin_port=htons(portnum);
if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("connect fail !\r\n");
return -1;
}
printf("connect ok !\r\n");
FILE *fp = fopen("qr.jpg", "rb");
if (fp == NULL)
{
fprintf(stderr, "cannot open image file\n");
exit(1);
}
fseek(fp, 0, SEEK_END);
if (ferror(fp)) {
fprintf(stderr, "fseek() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "cannot close file handler\n");
}
exit(1);
}
int flen = ftell(fp);
if (flen == -1) {
perror("error occurred");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "cannot close file handler\n");
}
exit(1);
}
fseek(fp, 0, SEEK_SET);
if (ferror(fp)) {
fprintf(stderr, "fseek() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "cannot close file handler\n");
}
exit(1);
}
char data[flen+1];
int size = fread(data, 1, flen, fp);
if (ferror(fp)) {
fprintf(stderr, "fread() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "cannot close file handler\n");
}
exit(1);
}
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "cannot close file handler\n");
}
printf("%d\n [%#x]\n", size, data);
if (PtTcpSnd(cfd, data, size) < 0) {
printf("sending on stream socket");
close(cfd);
exit(0);
}
close(cfd);
return 0;
}
small_hammer
- 粉丝: 2
- 资源: 4
最新资源
- ssm在线购书商城系统+vue.zip
- ssm在线云音乐系统的设计与实现+jsp.zip
- ssm园区停车管理系统+jsp.zip
- ssm影视企业全渠道会员管理系统的设计与实现+vue.zip
- ssm游戏攻略网站的设计与实现+vue.zip
- ssm医院住院综合服务管理系统设计与开发+vue.zip
- ssm亿互游在线平台设计与开发+vue.zip
- 三菱FX3U源码,三菱PLSR源码 总体功能和指令可能支持在RUN中下载程序,支持注释的写入和读取,有脉冲输出与定位指令(包括PLSY PWM PLSR PLSV DRVI DRVA 等指令)的代
- ssm应急资源管理系统+jsp.zip
- ssm医院门诊挂号系统+jsp.zip
- ssm医院住院管理系统+vue.zip
- ssm医用物理学实验考核系统+jsp.zip
- ssm学院学生论坛的设计与实现+vue.zip
- ssm医学生在线学习交流平台+vue.zip
- ssm亚盛汽车配件销售业绩管理统+jsp.zip
- 研控步进电机驱动器方案 验证可用,可以生产,欢迎咨询实际价格,快速掌握核心技术 包括硬件原理图 PCB源代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈