| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 679 人关注过本帖
标题:请高手帮忙看看这个程序!!
只看楼主 加入收藏
jq_spring
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-5-6
收藏
 问题点数:0 回复次数:3 
请高手帮忙看看这个程序!!
这是一个用winsock传送小文件的程序,每次都可以连接成功,可就是传东西的时候出错,请高手们指点一下!!! 好像是在send和recv的时候出的错! 首先是服务器程序: #include <winsock.h> #include <stdio.h> #include <io.h> #include <fcntl.h> #include <iostream.h> void fatal(char *string) { printf("%s\n", string); exit(1); } #define SERVER_PORT 12345 #define BUF_SIZE 4096 #define QUEUE_SIZE 10 void main(int argc, char *argv[]) { int b, I, sa; char buf[BUF_SIZE]; struct sockaddr_in channel; SOCKADDR saclnt; int saclntlen = sizeof(saclnt); int status; WSADATA wsaData; if(status = WSAStartup(MAKEWORD(1,1), &wsaData) != 0) exit(1); /* Build address structure to bind to socket. */ memset(&channel, 0, sizeof(channel)); /* zero channel */ channel.sin_family = AF_INET; channel.sin_addr.s_addr = htonl(INADDR_ANY); channel.sin_port = htons(SERVER_PORT); /* Passive open. Wait for connection. */ SOCKET s; s = socket(AF_INET, SOCK_STREAM, 0); /* create socket */ if (s < 0) fatal ("socket failed"); b = bind(s, (PSOCKADDR) &channel, sizeof(channel)); if (b < 0) fatal("bind failed"); I = listen(s, QUEUE_SIZE); /* specify queue size */ if (I < 0) fatal("listen failed"); sa = accept(s, &saclnt, &saclntlen); char *p_c; int count; int numchr; while (1){ p_c = buf; count = BUF_SIZE; numchr = 0; do { numchr = recv(sa,p_c,count,0); if (numchr == SOCKET_ERROR) { int err = WSAGetLastError(); cout << err << endl; } else { p_c += numchr; count -= numchr; } }while(count > 0); cout << buf << endl; } } 然后是客户端程序: #include <iostream.h> #include <winsock.h> #include <io.h> #include <stdio.h> #define SERVER_PORT 12345 #define BUF_SIZE 4096 #define ip "202.33.11.102" void fatal(char *string) { printf("%s\n", string); exit(1); } void main(int argc, char **argv) { int c, s, bytes; char buf[BUF_SIZE] = "sadfsdf"; struct sockaddr_in channel; int status; WSADATA wsaData; if(status = WSAStartup(MAKEWORD(1,1), &wsaData) != 0) exit(1); s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (s <0) fatal ("socket"); channel.sin_family= AF_INET; channel.sin_addr.s_addr = inet_addr(ip); channel .sin_port= htons(SERVER_PORT); c = connect(s, (struct sockaddr *) &channel, sizeof(channel)); if (c < 0) fatal ("connect failed"); char *p_c; int count; int numchr; p_c = buf; count = BUF_SIZE; numchr = 0; do { numchr = send (s,p_c,count,0); if (numchr == SOCKET_ERROR){ int err = WSAGetLastError(); cout << err << endl; } else { p_c += numchr; count -= numchr; } }while(count > 0); }
搜索更多相关主题的帖子: 服务器 include define 
2005-05-06 12:48
ywjucao
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-5-6
收藏
得分:0 
好复杂啊看不懂
2005-05-06 21:36
C++大粉丝
Rank: 4
等 级:贵宾
威 望:10
帖 子:477
专家分:0
注 册:2004-4-23
收藏
得分:0 
我调试了你的程序,主要的问题是在客户和/服务器端的那个循环(while)里,你自己好好检查一下,再有,希望你养成好的编程习惯.

I am a big fan of c plus plus.
2005-05-06 22:56
jq_spring
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-5-6
收藏
得分:0 
不好意思,我是初学socket编程的,还不太懂,这个程序有一部分是从别处看来的,能不能帮忙指明一下出错的原因,不胜感激!!
2005-05-07 19:04
快速回复:请高手帮忙看看这个程序!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019024 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved