c++ 中的客户端/服务器程序问题
我已经在基于 i=on windows 套接字的 C++ 中创建了一个客户端和服务器程序。 我有一些问题,当我执行程序时,服务器端没有出现消息,之后程序退出。数据正在发送到服务器,但没有响应。
我将我的代码粘贴在下面,
****Server code
===========****
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <conio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsdata;
int result = WSAStartup(MAKEWORD(2,2),&wsdata);
if(result != 0){
printf("%s","Unable to initilize windows socket\n");
getch();
return 1;
}
struct addrinfo *addrResult = NULL,hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
result = getaddrinfo(NULL,DEFAULT_PORT,&hints,&addrResult);
if(result != 0){
printf("%s","Error in getaddrInfo");
getch();
return 1;
}
SOCKET listenSocket = INVALID_SOCKET;
listenSocket = socket(addrResult->ai_family,addrResult->ai_socktype,addrResult->ai_protocol);
if(listenSocket == INVALID_SOCKET){
printf("%s","Error in creating socket object\n");
getch();
closesocket(listenSocket);
WSACleanup();
return 1;
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(27015);
if( bind(listenSocket,addrResult->ai_addr,
(int)addrResult->ai_addrlen) == SOCKET_ERROR){
//if(bind(listenSocket,(SOCKADDR*) &service,
// sizeof(service)) == SOCKET_ERROR){
printf("bind failed: %d\n", WSAGetLastError());
freeaddrinfo(addrResult);
closesocket(listenSocket);
WSACleanup();
getch();
return 1;
}
freeaddrinfo(addrResult);
if(listen(listenSocket,SOMAXCONN) == SOCKET_ERROR){
printf("%s","Error in listening socket");
getch();
closesocket(listenSocket);
WSACleanup();
}
SOCKET clientSocket = INVALID_SOCKET;
clientSocket = accept((listenSocket,NULL,NULL);
if(clientSocket == INVALID_SOCKET){
closesocket(listenSocket);
WSACleanup();
}
char recvbuf[DEFAULT_BUFLEN];
int iRecvResult, iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
do{
iRecvResult = 0;
iSendResult = 0;
iRecvResult = recv(clientSocket,recvbuf,recvbuflen,0);
if(iRecvResult > 0){
printf("Bytes received: %d\n", iRecvResult);
getch();
// Echo the buffer back to the sender
iSendResult = send(clientSocket, recvbuf, iRecvResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iRecvResult == 0){
printf("Connection closing...\n");
}
else{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}while(iRecvResult > 0);
getch();
return 0;
}
**client code
============**
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <conio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment(lib, "Ws2_32.lib")
int main(){
WSADATA wsdata;
WSAStartup(MAKEWORD(2,2),&wsdata);
struct addrinfo *addrResult = NULL,hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
int result = 0;
if(getaddrinfo("127.0.0.1",DEFAULT_PORT,
&hints,&addrResult)){
printf("%s","Error in getaddrInfo\n");
WSACleanup();
getch();
return 1;
}
SOCKET connectingSocket = INVALID_SOCKET;
connectingSocket = socket(addrResult->ai_family,addrResult->ai_socktype,
addrResult->ai_protocol);
if(connectingSocket == INVALID_SOCKET){
printf("%s","Error in creating socket\n");
freeaddrinfo(addrResult);
WSACleanup();
getch();
return 1;
}
if(connect(connectingSocket,addrResult->ai_addr, (int)addrResult->ai_addrlen) != 0){
closesocket(connectingSocket);
connectingSocket = INVALID_SOCKET;
WSACleanup();
}
freeaddrinfo(addrResult);
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
// Send an initial buffer
iResult = send(connectingSocket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(connectingSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection for sending since no more data will be sent
// the client can still use the connectingSocket for receiving data
iResult = shutdown(connectingSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(connectingSocket);
WSACleanup();
return 1;
}
// Receive data until the server closes the connection
do {
iResult = recv(connectingSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while (iResult > 0);
getch();
return 0;
}
非常感谢您的帮助,它在我的 n=机器上不起作用,有些人说这段代码在他们的机器上运行良好。我使用 Visual Studio 2005 来开发它,我的操作系统是 Windows 7。
I have created a client and server program in c++ based i=on windows sockets.
I have some problem that when i execute the program no message appears on the server end and after that the program exits.Data is sending to server but it is not responding back.
i am pasting my code below
****Server code
===========****
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <conio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsdata;
int result = WSAStartup(MAKEWORD(2,2),&wsdata);
if(result != 0){
printf("%s","Unable to initilize windows socket\n");
getch();
return 1;
}
struct addrinfo *addrResult = NULL,hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
result = getaddrinfo(NULL,DEFAULT_PORT,&hints,&addrResult);
if(result != 0){
printf("%s","Error in getaddrInfo");
getch();
return 1;
}
SOCKET listenSocket = INVALID_SOCKET;
listenSocket = socket(addrResult->ai_family,addrResult->ai_socktype,addrResult->ai_protocol);
if(listenSocket == INVALID_SOCKET){
printf("%s","Error in creating socket object\n");
getch();
closesocket(listenSocket);
WSACleanup();
return 1;
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(27015);
if( bind(listenSocket,addrResult->ai_addr,
(int)addrResult->ai_addrlen) == SOCKET_ERROR){
//if(bind(listenSocket,(SOCKADDR*) &service,
// sizeof(service)) == SOCKET_ERROR){
printf("bind failed: %d\n", WSAGetLastError());
freeaddrinfo(addrResult);
closesocket(listenSocket);
WSACleanup();
getch();
return 1;
}
freeaddrinfo(addrResult);
if(listen(listenSocket,SOMAXCONN) == SOCKET_ERROR){
printf("%s","Error in listening socket");
getch();
closesocket(listenSocket);
WSACleanup();
}
SOCKET clientSocket = INVALID_SOCKET;
clientSocket = accept((listenSocket,NULL,NULL);
if(clientSocket == INVALID_SOCKET){
closesocket(listenSocket);
WSACleanup();
}
char recvbuf[DEFAULT_BUFLEN];
int iRecvResult, iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
do{
iRecvResult = 0;
iSendResult = 0;
iRecvResult = recv(clientSocket,recvbuf,recvbuflen,0);
if(iRecvResult > 0){
printf("Bytes received: %d\n", iRecvResult);
getch();
// Echo the buffer back to the sender
iSendResult = send(clientSocket, recvbuf, iRecvResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iRecvResult == 0){
printf("Connection closing...\n");
}
else{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}while(iRecvResult > 0);
getch();
return 0;
}
**client code
============**
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <conio.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment(lib, "Ws2_32.lib")
int main(){
WSADATA wsdata;
WSAStartup(MAKEWORD(2,2),&wsdata);
struct addrinfo *addrResult = NULL,hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
int result = 0;
if(getaddrinfo("127.0.0.1",DEFAULT_PORT,
&hints,&addrResult)){
printf("%s","Error in getaddrInfo\n");
WSACleanup();
getch();
return 1;
}
SOCKET connectingSocket = INVALID_SOCKET;
connectingSocket = socket(addrResult->ai_family,addrResult->ai_socktype,
addrResult->ai_protocol);
if(connectingSocket == INVALID_SOCKET){
printf("%s","Error in creating socket\n");
freeaddrinfo(addrResult);
WSACleanup();
getch();
return 1;
}
if(connect(connectingSocket,addrResult->ai_addr, (int)addrResult->ai_addrlen) != 0){
closesocket(connectingSocket);
connectingSocket = INVALID_SOCKET;
WSACleanup();
}
freeaddrinfo(addrResult);
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
// Send an initial buffer
iResult = send(connectingSocket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(connectingSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection for sending since no more data will be sent
// the client can still use the connectingSocket for receiving data
iResult = shutdown(connectingSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(connectingSocket);
WSACleanup();
return 1;
}
// Receive data until the server closes the connection
do {
iResult = recv(connectingSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while (iResult > 0);
getch();
return 0;
}
Your help is highly appreciated it is not working on my n=machine some people are saying that this code is working well on their machines. I have used visual studio 2005 to develop this and my OS is windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在以下位置找到示例代码:
http://msdn.microsoft.com/en -us/library/ms738545%28v=vs.85%29.aspx
You can find a sample code at:
http://msdn.microsoft.com/en-us/library/ms738545%28v=vs.85%29.aspx