服务端:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include <arpa/inet.h>
#define MAXLINE 409600
int main(int argc, char** argv)
{
if(argc < 3)
{
printf("usage: ./%s <ip> <port>\n", basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int listenfd;
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
return 0;
}
printf("----init socket----\n");
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
inet_pton(AF_INET, ip, &servaddr.sin_addr);
//设置端口可重用
int contain;
setsockopt(listenfd,SOL_SOCKET, SO_REUSEADDR, &contain, sizeof(int));
if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
return 0;
}
printf("----bind sucess----\n");
if( listen(listenfd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
return 0;
}
FILE *fp;
if((fp = fopen("new1.jpg","ab") ) == NULL )
{
printf("File.\n");
close(listenfd);
exit(1);
}
printf("======waiting for client's request======\n");
int connfd;
char buff[MAXLINE];
while(1){
struct sockaddr_in client_addr;
socklen_t size=sizeof(client_addr);
// 调试时注意阻塞函数,如accept
if( (connfd = accept(listenfd, (struct sockaddr*)&client_addr, &size)) == -1){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
int n;
while(1){
n = recv(connfd, buff, MAXLINE, 0);
if(n == 0)
{
printf("n == 0, close connect.\n");
break;
}
else if( n < 0)
{ // #define EFAULT 14 /* Bad address */
printf("ERR: errno is %d\n", errno);
break;
}
fwrite(buff, 1, n, fp);
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
close(connfd);
fclose(fp);
}
close(listenfd);
return 0;
}
客户端:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MAXLINE 409600
int main(int argc, char** argv)
{
if( argc != 3){
printf("usage: ./%s <ipaddress> <port>\n", basename(argv[0]));
return 0;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int sockfd;
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
return 0;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
if( inet_pton(AF_INET, ip, &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
return 0;
}
if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
return 0;
}
// "r" "a" "w" 是针对文本文件的处理
// "rb" "ab" "wb" 都是针对二进制文件的处理(在POSIX系统上其实并无区别)
FILE *fq;
if( ( fq = fopen("test1.jpg","rb") ) == NULL ){
printf("File open.\n");
close(sockfd);
exit(1);
}
char buffer[MAXLINE];
int len;
memset(buffer,'\0',sizeof(buffer));
while(!feof(fq)){
len = fread(buffer, 1, sizeof(buffer), fq);
int rest = len;
while (rest > 0)
{
int send_ret = send(sockfd, buffer, len, 0);
rest -= send_ret;
}
}
close(sockfd);
fclose(fq);
return 0;
}
注:
1. 对二进制文件的读写最好使用fread/fwrite两个标准C函数
2. 对于二进制文件,一定要注意文件的大小(有可能具有成千上万bytes)
3. 对于socket TCP通信最好用recv()/send()来接收/发送数据