一个简单的socket问题 大家帮忙看看

chenxiaochen 2006-02-27 03:41:25
我写了一个简单的用socket通信的程序 可是 在accept函数这里总是过不去 我还如何解决呢?
SOCKET s=accept(m_hSocket,(LPSOCKADDR)&m_addr,(int*)sizeof(m_addr));这是我的那个函数

各位大虾 帮帮忙了
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenxiaochen 2006-03-10
  • 打赏
  • 举报
回复
谢谢 各位 已经解决了
playcats 2006-02-28
  • 打赏
  • 举报
回复
s
[in] A descriptor identifying a socket that has been placed in a listening state with the listen function. The connection will actually be made with the socket that is returned by accept.
addr
[out] An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family established when the socket was created.
addrlen
[out] An optional pointer to an integer that contains the length of the address addr.


1. (int*)sizeof(m_addr)):写法肯定有问题,把sizeof(m_addr)强制转化为(int*),如果sizeof(m_addr)=10,那么(int*)sizeof(m_addr))指向的内存地址为10,这应该不是正确的数据段所在的位置。应该使用&,而不是*;
2. 其中addr与addrlen是输出的,所以肯定不能是const,临时变量sizeof(m_addr)肯定不行。
quanbaoxiao 2006-02-28
  • 打赏
  • 举报
回复
SOCKET s=accept(m_hSocket,(LPSOCKADDR)&m_addr,(int)sizeof(m_addr));
Hylas 2006-02-28
  • 打赏
  • 举报
回复
上面已经是标准答案了;
我批示
「已注销」 2006-02-28
  • 打赏
  • 举报
回复
ListenSocket=socket(....);
bind(ListenSocket);
.........
sockaddr_in server_addr;
int rlen=sizeof(server);
ServerSocket=accept(ListenSocket,(sockaddr *)&server,&rlen);
fyx010641 2006-02-27
  • 打赏
  • 举报
回复
去MSDN看看吧 数据类型错!!
konj 2006-02-27
  • 打赏
  • 举报
回复
struct sockaddr_in ServerSocketAddr;
  int addrlen;
  addrlen=sizeof(ServerSocketAddr);
  ServerSocket=accept(ListenSocket,(struct sockaddr *)&ServerSocketAddr,&addrlen);

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧