server端:
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h> //sockaddr_in
-
-
#define PORT 6675
-
-
int main()
-
{
-
int socket_fd;
-
int new_fd;
-
//建立套接字
-
socket_fd=socket(AF_INET,SOCK_STREAM,0);// int socket(int domain, int type, int protocol);
-
printf("socket_fd is %d\n",socket_fd);
-
-
//初始化设置地址和端口号
-
struct sockaddr_in server_addr;
-
-
memset(&server_addr,0,sizeof(server_addr));
-
server_addr.sin_family=AF_INET;
-
server_addr.sin_port=htons(PORT);
-
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
-
memset(&(server_addr.sin_zero),0,8);
-
//设置端口可重用
-
int contain;
-
setsockopt(socket_fd,SOL_SOCKET, SO_REUSEADDR, &contain, sizeof(int));
-
-
//绑定fd和本机地址
-
int res=bind(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
-
printf("bind res is %d\n",res);
-
if(res==-1)
-
return -1;
-
-
if(listen(socket_fd,20)==-1)
-
{
-
printf("Failed to listen!\n");
-
return -1;
-
}
-
printf("after listen\n");
-
-
while(1)
-
{
-
struct sockaddr_in client_addr;
-
int size=sizeof(client_addr);
-
new_fd=accept(socket_fd, (struct sockaddr *)&client_addr, &size);
-
if(new_fd==-1)
-
{
-
printf("wait for client to connect!\n");
-
sleep(1);
-
continue;
-
}
-
printf("A connect and the new fd is %d\n",new_fd);
-
/*
-
while(1)
-
{
-
char input[100];
-
char output[100];
-
-
res=read(new_fd,input,100);
-
input[res]=0;
-
printf("client says '%s'\n",input);
-
-
gets(output);
-
res=write(new_fd,output,strlen(output));
-
}
-
*/
-
-
//while(1)
-
{
-
FILE *in;
-
char input[100];
-
res=read(new_fd,input,100);
-
input[res]=0;
-
char path[100];
-
sprintf(path,"/work/myproject/study/photo/get/%s",input);
-
printf("the path is %s\n",path);
-
in=fopen(path,"w");
-
printf("after path\n");
-
char photo[10];
-
while(1)
-
{
-
memset(photo,0,10);
-
res=read(new_fd,photo,10);
-
if(res==0)
-
return 0;
-
photo[res]=0;
-
int d=atoi(photo);
-
printf("data %d\n",d);
-
fputc(d,in);
-
}
-
}
-
}
-
close(socket_fd);
-
close(new_fd);
-
-
}
client端:
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <string.h>
-
-
#define SERVER_IP "127.0.0.1"
-
#define PORT 6675
-
-
int main()
-
{
-
int socket_fd;
-
socket_fd=socket(AF_INET,SOCK_STREAM,0);
-
-
struct sockaddr_in server_addr;
-
memset(&server_addr,0,sizeof(server_addr));
-
server_addr.sin_family=AF_INET;
-
server_addr.sin_port=htons(PORT);
-
server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
-
memset(&(server_addr.sin_zero),0,8);
-
-
int res =connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
-
printf("connect res is %d\n",res);
-
-
/*
-
while(1)
-
{
-
printf("send send ---->\n");
-
char input[100];
-
char output[100];
-
-
memset(input,0,sizeof(input));
-
gets(input);
-
res=write(socket_fd,input,strlen(input));
-
printf("the write resoult is %d\n",res);
-
-
res=read(socket_fd,output,100);
-
output[res]=0;
-
printf("server says:'%s'\n",output);
-
sleep(1);
-
}
-
*/
-
char path[100];
-
printf("please give the path of the photo\n");
-
char input[100];
-
gets(input);
-
sprintf(path,"/work/myproject/study/photo/%s",input);
-
write(socket_fd,input,strlen(input));
-
-
FILE *out=fopen(path,"r");
-
FILE *new=fopen("/work/myproject/study/photo/get/new.jpg","w");
-
int c;
-
sleep(1);
-
while((c=fgetc(out))!=EOF)
-
{
-
char photo[100];
-
sprintf(photo,"%d",c);
-
int b=atoi(photo);
-
fputc(b,new);
-
printf("data %d\n",b);
-
write(socket_fd,photo,strlen(photo));
-
usleep(1000);
-
}
-
char *end="#";
-
write(socket_fd,end,strlen(end));
-
close(socket_fd);
-
}
阅读(1996) | 评论(0) | 转发(0) |