HELP! Very Strange escape sequence problem

To make things simple: My program as a server recieves message from client using recv call. I use C++, GNU socket APIs under Ubuntu.

I let recv() recieve 1 charactor in the incoming message per iteration, in the recv loop. And if the recieved byte is an escape sequence such as "\n" and "\r", the server should detect it.
The problem is, sometimes it works and sometimes it fails....

following simplified code is what I did. Sometimes it works perfect, when "\n" or "\r" are recieved, the program will correctly output "n" or "r". However, sometimes it fails to detect the current recieved buffer equals to "\n" or "\r", while mbuffer IS "\n" or "\r".

The correct output is like:
---mbuffer: '
---'
---n

The failure output is like:
---mbuffer:'
---'


mbuffer must be an line break sequance such as "\n", because the second " ' " is in a new line each time the "mbuffer" is outputted. In the failure output, mbuffer is "\n" or "\r", but the program dose not output "n" or "r"

Is that because the strcmp() is not stable?????????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while(recv(clientSoket, mbuffer,1,0)>0)
{
.......
.......
cout<<"mbuffer:"<<"'"<<*mbuffer<<"'"<<endl;


if(strcmp(mbuffer,"\r"))
 {        
    cout<<"r"<<endl;
 }
else if(strcmp(mbuffer,"\n"))                     
{       
    cout<<"n"<<endl;
                        
}

............
............
}




Last edited on
Can you post a bit more code please. We'll need to see the declaration of mbuffer and if/how it's initialised before entering the loop.
The following is the way I declare mbuffer. I did not initialize it before go into recv loop

char *mbuffer=(char *)malloc(sizeof(char));

why I let size of mbuffer = 1*sizeof(char), is because everytime the recv is putting 1 charactor into mbuffer.

There is another detail. Within the loop, every time after recv puts one character it recieved into mbuffer, I append mbuffer's content into another buffer, which should be the main buffer holding all the content of the message from client. I use strcat(buffer, mbuffer); to to this task.

following is my program code so far. The thread entry function is threadEntry(void *).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
The Server Entry
*/
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>

#include <time.h>
#include <string.h>
#include <vector>
#include <unistd.h>
#include <iostream>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>
#include "MessageContext.h"
#include "Server.h"
#include <stdlib.h>
#include <errno.h>
#define MAX_BUFFER        1
#define MAX_BUFFER_TO_HOLD 256
#define SERVER_PORT        37454

using namespace std;



pthread_t listenerT;
int serverFd;
Server server;
typedef struct threadParameter
{
        int index;
        int clientSoket;
        pthread_t thread;

}PARA;

int numT; //number of thread
bool continue_;

bool mystrcmp(char *s1, const char *s2)
{
	int i=0;
	do{

		if(s1[i]!=s2[i])
			return false;



	}while(s1[i++]!='\0');
	return true;
}


void threadEntry_(void *ptr)
{

                PARA* para=(PARA *)ptr;
                ptr=NULL;
                //int index=para->index;

                int *clientSoket=(int*)malloc(sizeof(int));
                *clientSoket=para->clientSoket;
                int *numr=(int*)malloc(sizeof(int));
                    int *numn=(int*)malloc(sizeof(int));
                    int *byteRcved=(int*)malloc(sizeof(int));
                    *numr=0;
                    *numn=0;
                    *byteRcved=0;
                    int *cont=(int*)malloc(sizeof(int));
                    *cont=1;


                   pthread_t tid=para->thread;
                char *buffer=(char *)malloc(MAX_BUFFER_TO_HOLD*sizeof(char));
                char *mbuffer=(char *)malloc(MAX_BUFFER*sizeof(char));

                 memset((void *)buffer, 0, MAX_BUFFER_TO_HOLD);


                pthread_detach(tid);


cout<<"--------"<<endl;

                while(int tmp=recv(*clientSoket, mbuffer,MAX_BUFFER,0)>0&&continue_)
                {
cout<<"----"<<endl;
cout<<"mbuffer:"<<"'"<<*mbuffer<<"'"<<endl;
cout<<"numr:"<<*numr<<"numn:"<<*numn<<endl;
cout<<"cont"<<*cont<<endl;
if(mystrcmp(mbuffer,"\\"))
	cout<<"ASD"<<endl;


                        if(*cont==1)
                        {
                        *byteRcved=*byteRcved+1;
                        if(*byteRcved>=MAX_BUFFER_TO_HOLD)
                        {cout<<"out of band"<<endl;
                        break;}

                        if(mystrcmp(mbuffer,"\r"))
                        {        *numr=*numr+1;
cout<<"numr++:  "<<*numr<<endl;


                        }
                        else if(mystrcmp(mbuffer,"\n"))
                        {        *numn=*numn+1;
cout<<"numn++:  "<<*numn<<endl;
                        }
                        else
                        {        strcat(buffer, mbuffer);

                        }
                        if((*numr)>=3||(*numn)>=3)
                        {
cout<<"sent:"<<endl;
cout<<"'";
for(int i=0;i<strlen(buffer);i++)
        cout<<buffer[i];
cout<<"'"<<endl;
                                send(*clientSoket,(void *)buffer, strlen(buffer),0);
                                *cont=2;

                                if(errno!=0)
                                        cout<<"error "<<errno<<endl;


                        }

                        }

                        memset((void *)mbuffer, 0, MAX_BUFFER);
cout<<"----"<<endl;
                }




                memset(cont,0,1);
                memset((void *)mbuffer, 0, MAX_BUFFER);
                memset((void *)buffer, 0, MAX_BUFFER_TO_HOLD);
                cout<<"client exited"<<endl;

                if(buffer!=NULL)
                {        free(buffer);
                        buffer=NULL;
                }
                if(mbuffer!=NULL)
                {       free(mbuffer);
                        mbuffer=NULL;
                }

                close (*clientSoket);
                free(para);

        //        cout<<"cli"<<endl;
                free(clientSoket);
        //        cout<<"numr"<<endl;
                free(numr);
        //        cout<<"numn"<<endl;
                free(numn);
        //        cout<<"byteRcved"<<endl;
                free(byteRcved);
        //        cout<<"cont"<<endl;
                free(cont);
        //        cout<<"finish"<<endl;
//cout<<"------------"<<endl;
                pthread_exit(NULL);

}

void *threadEntry(void *ptr)
{
        threadEntry_(ptr);


}

void *listenerEntry(void *ptr)
{
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, (int*)0);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,(int*)0);


        numT=0;//initially,it contains a thread for detecting user EOF on server side
        int connectionFd;
        struct sockaddr_in servaddr;
        char timebuffer[MAX_BUFFER+1];
        time_t currentTime;

        serverFd=socket(AF_INET,SOCK_STREAM,0);
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family=AF_INET;
        servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
        servaddr.sin_port=htons(SERVER_PORT);

        bind(serverFd, (struct sockaddr *)&servaddr, sizeof(servaddr));

        listen(serverFd, 100);


        while(1){

                //struct sockaddr Caddr;
                connectionFd=accept(serverFd,(struct sockaddr*)NULL, NULL);

                //cout<<Caddr;
                numT++;

                PARA *para=(PARA*)malloc(sizeof(PARA));
                para->index=numT-1;
                para->clientSoket=connectionFd;


                pthread_create(&para->thread,NULL,threadEntry,(void *)para);


                cout<<"client "<<numT<<" connected"<<endl;




        }

        pthread_exit(NULL);

}


bool inputAvailable()  
{
  struct timeval tv={0L,0L};
  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(0, &fds);
  return select(1, &fds, NULL, NULL, &tv);
}



int main()
{
        cout<<"**************************************"<<endl;
        cout<<"press Enter to exit"<<endl;
        cout<<"**************************************"<<endl;
        pthread_create(&listenerT,NULL,listenerEntry,(void *)NULL);
        continue_=true;
        while(!inputAvailable())
        {
        }
        continue_=false;
        if(pthread_cancel(listenerT)!=0)
                cout<<"failed to kill listener thread"<<endl;


        pthread_join(listenerT,NULL);

        cout<<"server ended"<<endl;
        close (serverFd);
}

your mystrcmp() method is wrong, need to modify.
Use some existing string compare method, if you are not sure.......
Topic archived. No new replies allowed.