Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
I am going to code a chat application using windows socket programming in c++ in visual studio,My program crashed with the error message:

The thread 0x10c0 has exited with code 0 (0x0).
A buffer overrun has occurred in myclient.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.

For more details please see Help topic 'How to debug Buffer Overrun Issues'.
The program '[5472] myclient.exe' has exited with code 0 (0x0).


there is also another problam about my code,using this code client is the one who must start the chat so i believe it is not a reasonable chat! I don't know how to modify the code in order to let both side send there massages when they want
I'm sorry for my language shortcomings.
I will be grateful if you help me solve this problem

this is server side:
/////////////////////send &recieve ///////////////////////
....
C++
while(1)
{
  r=recv(accport,imsg,256,0);
  printf("client:");
  imsg[r]='\0';
  puts(imsg);
  //for (int i=0;r<r;i++)
  //imsg[i]='\0';

   printf("server:");
   gets(msg);
   send(accport,msg,256,0);
   // closesocket(accport);
		
}

...


this is client:
C++
//////////////////send &recieve///////////////
...
while(1)
 {
  gets_s(output);
  send(client,output,strlen(output),0);
  //closesocket(client);

  r=recv(client,input,strlen(input),0);
  printf("server:");
  input[r+1]='\0';
  puts(input);
	
  }
...
Posted
Updated 26-Oct-13 18:43pm
v3
Comments
Richard MacCutchan 27-Oct-13 3:59am    
If you want either side to be able to start the chat then each must act like a server as well as client.
Member 10314595 27-Oct-13 4:26am    
hello Richard
thanks for the guid
may you explain more?should i create a socket in client side and connect to it from server?
what will change in that case?I think the problem is inside while loop,i mean the sequence of instructions cause such a problem, ?I can change it in a way that server starts the chat but the main problem is still there,anyway there is a force for the starter!
Just imagin a real chat, in a chat box there is no force about starter...
in my code when one of the two sides sends massage when it is not his turn,the massage appears in other side only if he himself send a massage,but i want the massage to appear in other side az soon az sender presses the enter key...
I hope I have explained my problem well.
Richard MacCutchan 27-Oct-13 4:44am    
You need to have two threads running. One of them accepts keyboard input and sends it to the remote site, and the other listens for messages and displays them on the screen. Split your screen into two parts so the user can type in one part and read the incoming messages in the other.
Member 10314595 27-Oct-13 5:24am    
my god!
i know nothing about multi threading!
may you help me ?or
is there any good tutorial about it?
Richard MacCutchan 27-Oct-13 8:01am    
Sorry I'm not available for offline chats, I answer questions here in my spare time. If you need tutorials on any subject then try the CodeProject articles section, or Google.

1 solution

The error source is in this line of the client:
r=recv(client,input,strlen(input),0);

The string variable input is not initialized and so strlen() returns a random value. You must pass the size of the buffer minus one to reserve space for the terminating NULL byte:
r=recv(client,input,sizeof(input)-1,0);
if (r >= 0)
    input[r] = 0;

Do the same on the server side where you are passing the fixed buffer size of 256 (which must be 255).
 
Share this answer
 
Comments
Member 10314595 26-Oct-13 7:45am    
thank you so much Jochen

I'll be happy if someone help me to solve my second problem "sequence of massage passing!"
in this code sequence of chat is:
client
server
client
server....
and chat PERFORCE startS from client side
how should I solve this one?
Jochen Arndt 26-Oct-13 10:38am    
I did not really understand what your second problem is. Do you also want the server to start a chat? If yes, just implement client and server code on both sides. Then the side that wants to start a chat connects as client to the other side.
Member 10314595 26-Oct-13 12:16pm    
Jochen, Danke folgen mein Problem!!
I am really confused!
my program doesn't let server to start chat!and it doesn't let client or server send more than 1 massage Consecutive !but in a usual chat program each side can start and send massage whenever he wants and there is no special sequence for it!
I don't know, shoulde i use multithreading or sth!?
what do u mean by implementing code in both sides?
shoulde server connect to client via another socket?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900