server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <string.h>
#include <pthread.h>
#include <strings.h>
#include <signal.h>
#define SERV_PORT 1685
#define STRINGLEN 255
#define DATALENGTH 1024
#define IP "172.18.215.24"
void alarm_timeout();
/*数据的传输结构*/
struct DataInfo
{
char chRemotePath[STRINGLEN];
char chFileName[STRINGLEN];
int iFileLen;
int iDataLen;
char chData[DATALENGTH];
};
typedef struct DataInfo S_DataInfo;
int main(int argc, char **argv)
{
int iSockfd, iNewSockfd;
int remote_len;
char remote_ip[64];
struct sockaddr_in MyAddr;
struct sockaddr_in TheirAddr;
struct sockaddr_in cli_addr = {AF_INET};
int iSinSize;
S_DataInfo data, *buffer;
buffer = (S_DataInfo*)malloc(sizeof(S_DataInfo));
bzero(&MyAddr, sizeof(MyAddr));
bzero(&data, sizeof(data));
/*往data中填入测试数据*/
//strcpy(data.chData, "\"This sentence is from server.\"");
//data.iDataLen = strlen(data.chData);
if((iSockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket()");
exit(-1);
}
MyAddr.sin_family = AF_INET;
MyAddr.sin_port = htons(SERV_PORT);
MyAddr.sin_addr.s_addr = inet_addr(IP);
if(bind(iSockfd, (struct sockaddr_in *)&MyAddr, sizeof(struct sockaddr_in)) == -1)
{
close(iSockfd);
perror("bind()");
exit(-1);
}
if(listen(iSockfd, 10) == -1)
{
close(iSockfd);
perror("listen()");
exit(-1);
}
signal(SIGCLD,SIG_IGN);
for(;;)
{
iSinSize = sizeof(struct sockaddr_in);
printf("Waiting\n");
if((iNewSockfd = accept(iSockfd, (struct sockaddr_in *)&TheirAddr, &iSinSize)) == -1)
{
close(iSockfd);
perror("accept()");
exit(-1);
}
printf("Server: got connection from %s\n", inet_ntoa(TheirAddr.sin_addr));
int i = 0;
switch(fork())
{
case -1:
close(iSockfd);
close(iNewSockfd);
perror("fork()");
exit(-1);
case 0:
//sleep(120);
//printf("sleep ok\n");
while (i++ < 2)
{
/*
getpeername(iNewSockfd,(struct sockaddr *)&cli_addr,&remote_len);
strcpy(remote_ip,(char *)inet_ntoa(cli_addr.sin_addr));
printf("client ip is %s \n",remote_ip);
signal( SIGALRM, alarm_timeout );
alarm(200);
if(read(iNewSockfd, buffer, sizeof(S_DataInfo)) <= 0)
{
close(iSockfd);
close(iNewSockfd);
printf("read data error !!");
exit(-1);
}
alarm(0);
printf("read data success ,datalen = %d,data =[%s]\n",buffer->iDataLen,buffer->chData);
*/
//sleep(10);
sleep(11);
strcpy(data.chData, "server receive data ");
strcat(data.chData, buffer->chData);
data.iDataLen = strlen(data.chData);
if(write(iNewSockfd, &data, sizeof(S_DataInfo)) == -1)
{
close(iSockfd);
close(iNewSockfd);
perror("write()");
exit(-1);
}
//if( (data.iDataLen == 4) && (strcmp(data.chData,"exit") == 0) )
// break;
}
printf("send and receive is complete.\n");
close(iNewSockfd);
exit(0);
default:
/*父进程中处理*/
close(iNewSockfd);
}/*end switch*/
} /*end for(;;)*/
free(buffer);
return 0;
}
void alarm_timeout()
{
printf("alarm_timeout ok\n");
exit(0);
}
client.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_PORT 1685
#define STRINGLEN 255
#define DATALENGTH 1024
#define IP "172.16.215.149"
/*数据的传输结构*/
struct DataInfo
{
char chRemotePath[STRINGLEN];
char chFileName[STRINGLEN];
int iFileLen;
int iDataLen;
char chData[DATALENGTH];
};
typedef struct DataInfo S_DataInfo;
int main()
{
int sock;
struct sockaddr_in sin;
S_DataInfo data, *buffer;
buffer = (S_DataInfo*)malloc(sizeof(S_DataInfo));
bzero(&data, sizeof(data));
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("client socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(SERV_PORT);
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("client connect error");
return -1;
}
int i = 0;
//sleep(120);
printf("sleep ok\n");
while (i++ < 10)
{
data.iDataLen = 1;
data.chData[0] = i + 48;
//send
if(write(sock, &data, sizeof(S_DataInfo)) <= 0){
printf("send data %d error\n",i);
return -1;
}
printf("send data [%s] ok\n",data.chData);
//receive
/*
if (read(sock, buffer, sizeof(S_DataInfo)) <= 0){
printf("revdata %d error",i);
return -1;
}
printf("receive data [%s] ok\n",buffer->chData);
*/
}
close(sock);
}