TCP Socket
Client
'If you want to transmit .txt file , change the out.jpg to out.txt'
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXDATASIZE 2048
int main(int argc, char *argv[])
{
if(argc<3){
printf("Usage: %s <Servcer IP> <Port> <File Name>\n",argv[0]);
return 0;
}
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
const char *server_ip = argv[1];
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, server_ip, &serveraddr.sin_addr);
connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
printf("=====================服务器链接成功=====================\n");
char buf[MAXDATASIZE];
char *filename=argv[3];
printf("Request File :%s\n",filename);
send(sockfd,filename,sizeof(filename),0);
memset(buf, '\0', MAXDATASIZE/sizeof (char));
FILE* fd;
int recvNum;
fd=fopen("out.jpg","wb");
while((recvNum=recv(sockfd,buf,sizeof(buf),0))>0){
printf("Receive: %d\n",recvNum);
fwrite(buf,recvNum,1,fd);
memset(buf, '\0', MAXDATASIZE/sizeof (char));
}
close(sockfd);
fclose(fd);
return 0;
}
Server
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <fcntl.h>
#define BACKLOG 10
#define MAXDATASIZE 2048
int main(int argc, char *argv[])
{
if(argc!=2){
printf("Usage: %s <Port> \n",argv[0]);
return 0;
}
int listenfd;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[1]));
serveraddr.sin_addr.s_addr = INADDR_ANY;
bind(listenfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
listen(listenfd, BACKLOG);
printf("======bind success,waiting for client's request on port:%s======\n",argv[1]);
struct sockaddr_in peeraddr;
socklen_t peer_len = sizeof(peeraddr);
int connfd;
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)&peeraddr, &peer_len);
printf("\n=====================客户端链接成功=====================\n");
printf("IP = %s:PORT = %d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
char buf[MAXDATASIZE];
while(1)
{
memset(buf, '\0', MAXDATASIZE/sizeof (char));
int recv_length = recv(connfd, buf, MAXDATASIZE/sizeof (char), 0);
if(recv_length == 0)
{
printf("client has closed!\n");
break;
}
int len,nread;
FILE* fd;
printf("request file name: %s\n",buf);
fd=fopen(buf,"rb");
fseek(fd,0,SEEK_END);
len=ftell(fd);
fseek(fd,0,SEEK_SET);
printf("file length: %d\n",len);
while((nread=fread(buf,1,MAXDATASIZE/sizeof(char),fd))>0){
int sendNum;
if((sendNum=send(connfd,buf,nread,0))!=nread){
printf("sending error %d\n",sendNum);
}
printf("sending data : %d\n",sendNum);
memset(buf, '\0', MAXDATASIZE/sizeof (char));
}
fclose(fd);
shutdown(connfd,SHUT_WR);
recv(connfd,buf,MAXDATASIZE/sizeof (char), 0);
}
close(connfd);
close(listenfd);
return 0;
}
}
|