#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<netdb.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MAXDATASIZE 100
int main()
{
int sockfd,client_fd,sin_size;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[MAXDATASIZE];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{perror("socket");exit(1);}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1)
{perror("bind");exit(1);}
if(listen(sockfd,BACKLOG)==-1)
{perror("listen");exit(1);}
sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr*)&remote_addr,&sin_size))==-1)
{perror("accept");exit(1);}
printf("received a connection from %d\n",inet_ntoa(remote_addr.sin_addr));
FILE *fd=fopen("zj.jpg","rb");
if(fd==NULL)
{
printf("file open error!\n");
exit(2);
}
while(!feof(fd))
{
int len=fread(buf,1,MAXDATASIZE,fd);
send(client_fd,buf,len,MSG_NOSIGNAL);
}
close(client_fd);
fclose(fd);
return 0;
}