- 论坛徽章:
- 0
|
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(int argc, char * argv[])
{
struct sockaddr_in sAddr;
int nConnectSocket;
int nFlag = 1,nRet = 0;
int nPort = 7777;
char * sIP = "10.12.8.154";
char sTempBuff[100] = {0};
memset(&sAddr, 0, sizeof(sAddr));
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(nPort);
sAddr.sin_addr.s_addr = inet_addr(sIP);
for(int i = 1; i <100; i++)
{
sleep(1);
if(fork() == 0) //子进程
{
printf("Child process:%d\n",getpid());
break;
}
}
nConnectSocket = socket(AF_INET, SOCK_STREAM, 0);
if (nConnectSocket < 0)
{
printf("New socket failed:%s!",strerror(errno));
return -1;
}
if (connect(nConnectSocket,(struct sockaddr*)&sAddr, sizeof(sockaddr)) == -1)
{
printf("Connect redis server(%s:%d) failed:%s!",sIP,nPort,strerror(errno));
close(nConnectSocket);
return -1;
}
sprintf(sTempBuff,"*2\r\n$6\r\nEXISTS\r\n$31\r\nt_cp_blacklist_user:13502399876\r\n");
while (1)
{
nRet = send(nConnectSocket,sTempBuff,strlen(sTempBuff),0);
if (nRet <= 0)
{
printf("Send failed\n");
close(nConnectSocket);
break;
}
char recvBuff[100] = {0};
nRet = recv(nConnectSocket,recvBuff,sizeof(recvBuff),0);
if (nRet <= 0)
{
printf("Recv failed\n");
}
else
{
printf("Recv:%s\n",recvBuff);
}
}
return 0;
}
|
|