Chinaunix首页 | 论坛 | 博客
  • 博客访问: 534789
  • 博文数量: 95
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 1202
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-20 01:23
文章分类

全部博文(95)

文章存档

2010年(28)

2009年(67)

我的朋友

分类: C/C++

2009-08-10 15:01:02

socket send问题? 为什么会出现? send()及recv()返回值问题

时间:2008-07-05 12:08:10   来源:论坛整理  作者:  编辑:chinaitzhe
send()及recv() 的返回值
-1 SOCKER_ERROR
0 SOCKER 连接失败
>0
是否只需要这几种情况 我的程序在阻塞模式下的.

代码如下,有点乱 不好意思
服务端:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 9527 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
#define MAXSIZE 100*10*1024
main()
{
int sockfd,client_fd;
int sin_size;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
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);
}

while(1) {
sin_size = sizeof(struct sockaddr_in);
FILE *fp;
int fseet; //文件位置
char tempbuff[162];
fp=fopen("./TICK.txt","r");
char sendBuff[MAXSIZE];

bufclr(sendBuff);
strcpy(sendBuff,"");
char tempChar[162];
bufclr(tempChar);
int p=0;
while(fgets(tempChar,162,fp) != NULL){
if(strlen(tempChar)>3){
tempChar[161]=0;
strcat(sendBuff,tempChar);
bufclr(tempChar);
}
}

for(;;){
printf("LEN=%d\n",strlen(sendBuff));
client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size);
if (client_fd < 0) {
perror("accept出错");
sleep(2);
continue;
}

printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));

fseet=0;
char buffer[100];
char _Buff[1024];
int received;

while(1){
if(fseet>=strlen(sendBuff))
fseet=0;
bufclr(tempbuff);
memcpy(tempbuff,sendBuff fseet,161);
bufclr(buffer);
printf("等待数据........\n");
if ((received = recv(client_fd, buffer, 1024, 0)) < 0) {
if (errno == ECONNRESET) {
printf("recv error received :%d errno: %d\n",received,errno);
close(sockfd);
} else
printf("recv error received :%d errno: %d\n",received,errno);
sleep(2);
break;
} else if (received == 0) {
close(sockfd);
printf("recv error received :%d errno: %d close sock\n",received,errno);
break;
} else {
buffer[received]=0;
printf("recv = %s\n",buffer);
}

bufclr(_Buff);
sprintf(_Buff,"%sd%s",buffer,strlen(buffer) strlen(tempbuff) 4,tempbuff);

received = 0;
for(;;){
if ((received = send(client_fd, _Buff, strlen(_Buff), 0)) < 0){
if (errno == ECONNRESET) {
printf("send error received :%d errno: %d\n",received,errno);
close(sockfd);
break;
} else
printf("send error received :%d errno: %d\n",received,errno);
sleep(2);
break;
} else if (received == 0) {
close(sockfd);
printf("send error received :%d errno: %d close sock\n",received,errno);
break;
} else {
printf("send = %s\n",_Buff);
}
break;
}
fseet =161;
}


}

fclose(fp);
close(client_fd);
}
exit(0);
}

//初始化内存
bufclr(char *p){
memset(p,1,sizeof(p));
}



网友回复:客户端:
/*************************************************************
*createSHM(): 开辟共享内存空间
*initSHM(): 获得共享内存空间
*bufclr(): 初始化字符串内存
*recvPrice() 接收价格
*initFile() 产生价格文件
*initSleep() 睡眠时间
*initSocket() 接收数据
**************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define PROC_SIZE 1 //进程数
#define BUFF_SIZE 1024*10 //
#define SERVER_IP "192.168.1.104" //服务器 IP
#define SERVER_PORT "9527" //服务器端口
#define SHEM_SIZE 1024 //开辟共享内存大小
#define SHEM_KEY 13800 //共享内存 key 值
#define SEM_KEY 13801 //信号量 key 值
#define EMPFILE_SIZE 3600 //临时文件个数(存放价格)
#define SLEEP_TIME 1000000 //发送价格频率(微秒)

FILE *fp; //文件指针
int fsize = 0; //写入文件大小
char fileName[100]; //文件名
char *shemBuff; //共享变量
int pubsemid; //信号量变量

bufclr(char *p){
p = malloc(sizeof(p));
}

int createSHM(key_t key,int size){
int shmid;
shmid = shmget(key, size, IPC_CREAT ¦0666);
if (shmid == -1) {
fprintf(stderr, "create the Key[%d] share momery error\n",key);
return -1;
}

shemBuff = (char *)shmat(shmid, 0, 0);
if (shemBuff == (void *)-1) {
fprintf(stderr, "attach shared momery error\n");
return -1;
}
return 0;
}

int initSEM(key_t key){
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
union semun sem;
int semid;
sem.val=0;
semid = semget(key,1,IPC_CREAT ¦0666);
if (semid == -1){
printf(" create semaphore error\n" );
exit(-1);
}
//初始化信号量
semctl(semid,0,SETVAL,sem);
return semid;
}

int sem_p(int semid){
struct sembuf sops={0, 1,IPC_NOWAIT};
return(semop(semid,&sops,1));
}

int sem_v(int semid){
struct sembuf sops={0,-1,IPC_NOWAIT};
return(semop(semid,&sops,1));
}

/*ptno 产品编号
*buff 内容
*shmBuff 共享内存变量*/
int initFile(char *ptno,char *buff,int _ct){
struct tm *tt;
time_t t = time(NULL);
char f_time[9];

bufclr(fileName);

bufclr(f_time);
tt = gmtime(&t);
sprintf(f_time,"d:d:d",tt->tm_hour,tt->tm_min,tt->tm_sec);

sprintf(fileName,"./data/%s%sd",f_time,ptno,_ct);

if ((fp = fopen(fileName, "w")) == NULL){
fprintf(stderr, "fopen() %s fail.\n",fileName);
return -1;
}

fsize = (int)malloc(sizeof(int));

if(fsize = fwrite(buff, strlen(buff),1, fp) <=0){
fprintf(stderr, "fwrite() %s fail.\n",fileName);
return -1;
}
fclose(fp);

sem_p(pubsemid);
bufclr(shemBuff);
sprintf(shemBuff,"%s%sd",f_time,ptno,_ct);
sem_v(pubsemid);

printf("shemBuff=[%s]\n",shemBuff);
return 0;
}

initSocket(char *ptno){
int sock;
struct sockaddr_in echoserver;
char recvbuffer[BUFF_SIZE]; //接收字符
char sendBuffer[BUFF_SIZE]; //发送字符
char tempBuffer[1024];
int fileName_ct = 0; //当前文件数
char szecholen[5]; //获取返回长度
unsigned int echolen; //长度
int received = 0;
int i,bytes = 0;

struct timeval tpstart,tpend;
float timeuse;
int t_e = 0;

if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr,"socket() fail . \n");
return -1;
}

memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_port = htons(atoi(SERVER_PORT));
echoserver.sin_addr.s_addr = inet_addr(SERVER_IP);

for(;;){

printf("等待连接... \n");
if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0) {
fprintf(stderr,"connect() fail . \n usleep(200000) . \n");
usleep(200000); //睡眠0.2秒
continue;
}

bufclr(sendBuffer);
strcpy(sendBuffer,ptno);
echolen = strlen(sendBuffer);

fileName_ct = 1;

for(;;){
gettimeofday(&tpstart,NULL);

if(fileName_ct>3600)
fileName_ct=1;

printf("等待发送... \n");
bytes = send(sock, sendBuffer, echolen, 0);
if(bytes < 0){
if (errno == ECONNRESET) {
printf("send error bytes :%d errno : %d close sock\n",bytes,errno);
close(sock);
} else
printf("send error bytes :%d errno : %d close sock\n",bytes,errno);

sleep(2);
break;
} else if (bytes == 0) {
close(sock);
printf("send error bytes :%d errno : %d close sock\n",bytes,errno);
break;
} else {
printf("send = %s\n",sendBuffer);
}


if(bytes!=echolen) {
fprintf(stderr,"发送的数据与实际数据长度不一致\n");
sleep(2);
continue;
}else{
if(bytes == 0){
fprintf(stderr,"发送数据到服务器失败 errno=%d \n",bytes);
usleep(2000000); //睡眠0.2秒
continue;
}
if(bytes < 0){
fprintf(stderr,"发送数据到服务器失败 errno=%d \n",bytes);
close(sock);
usleep(200000); //睡眠0.2秒
break;
}
}

received = 0;
echolen = 1;
bufclr(recvbuffer);

while (received < echolen) {
printf("等待接收... \n");
fprintf(stderr,"received = %d echolen = %d \n",received,echolen);

bytes = recv(sock, tempBuffer, BUFF_SIZE-1, 0);
if(bytes <0){
if (errno == ECONNRESET) {
printf("recv error bytes :%d errno : %d close sock\n",bytes,errno);
close(sock);
} else
printf("recv error bytes :%d errno : %d close sock\n",bytes,errno);

sleep(2);
break;
} else if (bytes == 0) {
close(sock);
printf("recv error bytes :%d errno : %d close sock\n",bytes,errno);
break;
} else {
tempBuffer[bytes]=0;
printf("recv = %s\n",tempBuffer);
}


if (bytes == 0) {
fprintf(stderr,"fd:%d recv() fail . errno=%d \n",sock,bytes);
usleep(2000000); //睡眠0.2秒
continue;
}else if(bytes < 0){
fprintf(stderr,"fd:%d recv() fail . errno=%d \n",sock,bytes);
usleep(2000000); //睡眠0.2秒
break;
}

//获取包体大小
if(received==0 && bytes > 0){
bufclr(szecholen);
memcpy(szecholen,tempBuffer strlen(ptno),4);
printf("szecholen = [%s]\n",szecholen);
echolen = atoi(szecholen);
}

received = bytes;
printf("bytes = [%d] \n",bytes);
strcat(recvbuffer,tempBuffer);
bufclr(tempBuffer);
}

for(i=0;i <3;i ){
printf("写入文件及共享内存\n");
if(initFile(ptno,recvbuffer,fileName_ct)!=0){
fprintf(stderr, "InitFile() fail.\n");
usleep(2000000); //睡眠0.2秒
continue;
}
break;
}

fileName_ct ;

gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec - tpstart.tv_sec) tpend.tv_usec - tpstart.tv_usec;
t_e = SLEEP_TIME - timeuse;
fprintf(stderr, "timeuse = %d \t SLEEP_TIME= %d \t Sleep = %d \n\n",timeuse,SLEEP_TIME,t_e);
usleep (t_e);

}

close(sock);
}
printf("end \n");
return 0;
}

网友回复:
int recvPrice(){
char priceBuff[BUFF_SIZE];
char ptno[4 1]; //产品编号
int i = 0;
int fkid;

bufclr(ptno);
bufclr(shemBuff);
bufclr(priceBuff);

//测试用
strcpy(ptno,"PT01");

//开辟共享内存
if(createSHM(SHEM_KEY,SHEM_SIZE)!=0){
fprintf(stderr, "CreateSHM() fail.\n");
return -1;
}
pubsemid=initSEM(SEM_KEY);

for(i=0;i fkid = fork();

if(fkid>0){ //父
return 0;
}
else if(fkid==0){//子
initSocket(ptno);
}
else{ //出错
fprintf(stderr, "fork() fail .\n");
return -1;
}
}
return 0;
}

main(){
printf("RecvPrich Process Start .\n");
recvPrice();
exit(0);
}

服务端运行后LOG
LEN=160839
received a connection from 192.168.1.104
等待数据........
recv = PT01
send = PT010169ASK_TICKBS;GBPUSD ¦1.9728 ¦1.9728;USDCAD ¦1.0169 ¦1.0167;USDJPY ¦104.7500 ¦104.7100;USDCHF ¦1.0412 ¦1.0412;EURUSD ¦1.5580 ¦1.5580;USDHKD ¦7.8029 ¦7.8029;AUDUSD ¦0.9484 ¦0.9484
等待数据........
recv = PT01
send error received :-1 errno: 88
等待数据........
recv error received :-1 errno: 88
LEN=160839
accept出错: Socket operation on non-socket
LEN=160839
accept出错: Socket operation on non-socket
LEN=160839
accept出错: Socket operation on non-socket
LEN=160839
accept出错: Socket operation on non-socket
LEN=160839
accept出错: Socket operation on non-socket

客户端运行后结果
RecvPrich Process Start .
等待连接...
等待发送...
send = PT01
等待接收...
received = 0 echolen = 1
[system@localhost gdbc]$ recv = PT010169ASK_TICKBS;GBPUSD ¦1.9728 ¦1.9728;USDCAD ¦1.0169 ¦1.0167;USDJPY ¦104.7500 ¦104.7100;USDCHF ¦1.0412 ¦1.0412;EURUSD ¦1.5580 ¦1.5580;USDHKD ¦7.8029 ¦7.8029;AUDUSD ¦0.9484 ¦0.9484
szecholen = [0169]
bytes = [169]
写入文件及共享内存
shemBuff=[07:05:18PT010001]
timeuse = 0 SLEEP_TIME= 1094658561 Sleep = 1000000


[system@localhost gdbc]$ make RecvPrich
cc RecvPrich.c -o RecvPrich
[system@localhost gdbc]$ ./RecvPrich
RecvPrich Process Start .
等待连接...
等待发送...
send = PT01
等待接收...
received = 0 echolen = 1
recv = PT010169ASK_TICKBS;GBPUSD ¦1.9728 ¦1.9728;USDCAD ¦1.0169 ¦1.0167;USDJPY ¦104.7500 ¦104.7100;USDCHF ¦1.0412 ¦1.0412;EURUSD ¦1.5580 ¦1.5580;USDHKD ¦7.8029 ¦7.8029;AUDUSD ¦0.9484 ¦0.9484
szecholen = [0169]
bytes = [169]
写入文件及共享内存
[system@localhost gdbc]$ shemBuff=[07:07:52PT010001]
timeuse = 0 SLEEP_TIME= 1088600928 Sleep = 1000000

等待发送...
send = PT01
等待接收...
received = 0 echolen = 1

运行环境LIUNX 内核2.6

网友回复:分不够可加
网友回复:GetLastError()
网友回复:我的程序是客户端定时发送请求数据,将服务端收的数据写入文件及共享内存.
客户端第一次 SEND 及 RECV 都正常
第二次 SEND 正常 RECV 等待中出错.

服务端 第二次 RECV 正常 SEND 错误 errno: 88
阅读(5240) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~