Server:等待连接,有连接进来的时候,就accept,新fork一个子进程,然后read,再将得到的buf存到getpid().txt,父进程继续accept。
Client:连接,然后传送指定文件到Server。
Server:s.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
// ./s 8888 5
// exe portn n
int count;
int init(char *ipaddr, int n) //
{
int s;
struct sockaddr_in s_addr;
int len;
s = socket(AF_INET, SOCK_STREAM, 0);
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(ipaddr));
s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(s_addr);
bind(s, (struct sockaddr *)&s_addr, len);
listen(s, n);
printf("Server init succeed!\n");
return s;
}
int main(int argc, char *argv[])
{
struct sockaddr_in c_addr;
int cn;
int l;
count=0;
int s = init(argv[1], atoi(argv[2]) );
while(1)
{
cn = accept(s, (struct sockaddr *)&c_addr,&l);
if(fork()==0)
{
printf("accept!\n");
char buf[1024];
int len = read(cn, buf,1024);
if(len<0)
{
printf("error!\n");
}else
{
char filename[32];
snprintf(filename, 32, "%d.txt", getpid());
int fd=open(filename, O_CREAT|O_RDWR, 0600);
write(fd, buf, len);
close(fd);
}
}
close(cn);
}
return 0;
}
gcc s.c -o s
Client:c.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
struct sockaddr_in s_addr,c_addr;
int len;
int x;
int s;
char buf[1024];
int fd;
s = socket(AF_INET, SOCK_STREAM, 0);
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &s_addr.sin_addr);
if (connect(s, (struct sockaddr*)&s_addr, sizeof(struct sockaddr) ) <0)
{
printf("connect fail!\n");
return 0;
}
fd=open(argv[3], O_CREAT|O_RDWR, 0666);
x = read(fd, buf,1024);
write(s, buf, x);
close(fd);
printf("send %s to server!\n", argv[3]);
return 0;
}
gcc c.c -o c
运行:
administrator@ubuntu:~/test/net/new$ ./s 8888 10 & [1] 4661 administrator@ubuntu:~/test/net/new$ Server init succeed! administrator@ubuntu:~/test/net/new$ ./c 127.0.0.1 8888 c.c client: send c.c to server! accept! server: save 833 Bytes to 4666.txt administrator@ubuntu:~/test/net/new$ ll 4666.txt -rw------- 1 administrator administrator 833 2013-05-17 14:17 4666.txt administrator@ubuntu:~/test/net/new$
administrator@ubuntu:~/test/net/new$ cat 4666.txt #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <fcntl.h> int main(int argc, char *argv[]) { struct sockaddr_in s_addr,c_addr; int len; int x; int s; char buf[1024]; int fd; s = socket(AF_INET, SOCK_STREAM, 0); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(atoi(argv[2])); inet_pton(AF_INET, argv[1], &s_addr.sin_addr); if (connect(s, (struct sockaddr*)&s_addr, sizeof(struct sockaddr) ) <0) { printf("connect fail!\n"); return 0; } fd=open(argv[3], O_CREAT|O_RDWR, 0666); x = read(fd, buf,1024); write(s, buf, x); close(fd); printf("client:\tsend %s to server!\n", argv[3]); return 0; } administrator@ubuntu:~/test/net/new$