c语言socket传输图片
时间: 2023-10-25 12:10:15 浏览: 268
socket传输图片
4星 · 用户满意度95%
在C语言中使用Socket传输图片,可以按照以下步骤进行:
1. 打开图片文件并读取图片数据到内存中。
2. 创建Socket并连接到目标主机的Socket端口。
3. 将图片数据通过Socket发送到目标主机。
4. 目标主机接收到数据后,保存为图片文件。
以下是一个简单的示例代码:
发送端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_PORT 8888
#define MAXLINE 4096
int main(int argc, char **argv)
{
int sockfd, n;
char sendline[MAXLINE], recvline[MAXLINE];
struct sockaddr_in servaddr;
if (argc != 2) {
printf("usage: ./client <ipaddress>\n");
exit(0);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("create socket error\n");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("inet_pton error for %s\n", argv[1]);
exit(0);
}
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
printf("connect error\n");
exit(0);
}
char *filename = "test.jpg";
FILE *fp = fopen(filename, "rb");
if (!fp) {
printf("open file error\n");
exit(0);
}
fseek(fp, 0, SEEK_END);
int filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *imgdata = (char *)malloc(filesize);
if (!imgdata) {
printf("malloc error\n");
exit(0);
}
fread(imgdata, 1, filesize, fp);
fclose(fp);
n = write(sockfd, (char *)&filesize, sizeof(filesize));
n = write(sockfd, imgdata, filesize);
free(imgdata);
close(sockfd);
return 0;
}
```
接收端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_PORT 8888
#define MAXLINE 4096
int main(int argc, char **argv)
{
int listenfd, connfd, n;
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
char buf[MAXLINE];
char *filename = "recv.jpg";
FILE *fp;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("create socket error\n");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
printf("bind error\n");
exit(0);
}
if (listen(listenfd, 10) == -1) {
printf("listen error\n");
exit(0);
}
printf("waiting for client's request...\n");
while (1) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
printf("accept a new client: %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
int filesize;
n = read(connfd, &filesize, sizeof(filesize));
printf("image size: %d\n", filesize);
fp = fopen(filename, "wb");
if (!fp) {
printf("open file error\n");
exit(0);
}
int len = 0;
while (len < filesize) {
n = read(connfd, buf, MAXLINE);
fwrite(buf, 1, n, fp);
len += n;
}
fclose(fp);
close(connfd);
printf("received image saved as %s\n", filename);
}
close(listenfd);
return 0;
}
```
在这个示例中,我们使用TCP协议进行Socket通信,并且使用了JPEG格式的图片。注意在实际应用中,需要根据具体情况选择合适的协议和编码方式。
阅读全文