聊天应用程序(winsock编程) [英] chat application (winsock programming)

查看:67
本文介绍了聊天应用程序(winsock编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我将在Visual Studio中使用c ++中的Windows套接字编程编写聊天应用程序,我的程序崩溃并显示错误消息:



线程0x10c0已退出,代码为0(0x0)。

myclient.exe中发生缓冲区溢出,该程序已损坏程序的内部状态。按Break调试程序或继续终止程序。



有关详细信息,请参阅帮助主题如何调试缓冲区溢出问题。

程序'[5472] myclient.exe'退出时代码为0(0x0)。




还有另一个关于我的代码的问题,使用此代码客户端必须开始聊天的人,所以我认为这不是一个合理的聊天!我不知道如何修改代码,以便让双方在他们想要的时候发送按摩

我很抱歉我的语言缺点。

我将是如果你能帮我解决这个问题,我很感激



这是服务器端:

///////////// ////////发送和接收///////////////////////

....

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 ///////////////////////
....

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:

//////////////////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);
	
  }
...

推荐答案

错误源位于客户端的这一行:

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



字符串变量 input 未初始化,因此 strlen()返回一个随机值。您必须将缓冲区的大小减去1以保留终止空字节的空间:


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;



在您通过的服务器端执行相同操作固定缓冲区大小为256(必须为255)。


Do the same on the server side where you are passing the fixed buffer size of 256 (which must be 255).


这篇关于聊天应用程序(winsock编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆