我买的windows网络编程书中的第1个程序。我在局域网上的2个机器上用了可用了总是没看出作用呀,也就是说在局域网上的2个机器上怎么测试一下呢?达到预计显示的效果呢?谁说说原因和解决办法。
// Module Name: tcpclient.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP client application
// that can send a simple "hello" message to a TCP server listening on port 5150.
// This sample is implemented as a console-style application and simply prints
// status messages a connection is made and when data is sent to the server.
//
// Compile:
//
// cl -o tcpclient tcpclient.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpclient.exe <server IP address>
//#include <winsock2.h>
#include <stdio.h>void main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET s;
SOCKADDR_IN ServerAddr;
int Port = 5150;
int Ret; if (argc <= 1)
{
printf("USAGE: tcpclient <Server IP address>.\n");
return;
} // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to make a client connection.
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}
// Setup a SOCKADDR_IN structure that will be used to connect
// to a listening server on port 5150. For demonstration
// purposes, we required the user to supply an IP address
// on the command line and we filled this field in with the
// data from the user. ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr(argv[1]); // Make a connection to the server with socket s. printf("We are trying to connect to %s:%d...\n",
inet_ntoa(ServerAddr.sin_addr), htons(ServerAddr.sin_port)); if (connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("connect failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("Our connection succeeded.\n");
// At this point you can start sending or receiving data on
// the socket s. We will just send a hello message to the server. printf("We will now try to send a hello message.\n"); if ((Ret = send(s, "Hello", 5, 0)) == SOCKET_ERROR)
{
printf("send failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("We successfully sent %d byte(s).\n", Ret); // When you are finished sending and receiving data on socket s,
// you should close the socket. printf("We are closing the connection.\n"); closesocket(s); // When your application is finished handling the connection, call
// WSACleanup. WSACleanup();
}// Module Name: tcpserver.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP server application
// that listens for a TCP connection on port 5150 and receives data. This
// sample is implemented as a console-style application and simply prints
// status messages when a connection is accepted and when data is received
// by the server.
//
// Compile:
//
// cl -o tcpserver tcpserver.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpserver.exe
//
// NOTE: There are no command parameters.
//
#include <winsock2.h>
#include <stdio.h>void main(void)
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 5150;
int Ret;
char DataBuffer[1024]; // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to listening for client connections.
if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
} // Setup a SOCKADDR_IN structure that will tell bind that we
// want to listen for connections on all interfaces using port
// 5150. Notice how we convert the Port variable from host byte
// order to network byte order. ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Associate the address information with the socket using bind. if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("bind failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
} // Listen for client connections. We used a backlog of 5 which is
// normal for many applications. if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("listen failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
} printf("We are awaiting a connection on port %d.\n", Port); // Accept a new connection when one arrives. if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
printf("We successfully got a connection from %s:%d.\n",
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port)); // At this point you can do two things with these sockets. Await
// for more connections by calling accept again on ListeningSocket
// and start sending or receiving data on NewConnection. For
// simplicity We will stop listening for more connections by closing
// ListeningSocket. We will start sending and receiving data on
// NewConnection.
closesocket(ListeningSocket); // Start sending and receiving data on NewConnection. For simplicity,
// we will just receive some data and report how many bytes were
// received. printf("We are waiting to receive data...\n"); if ((Ret = recv(NewConnection, DataBuffer, sizeof(DataBuffer), 0))
== SOCKET_ERROR)
{
printf("recv failed with error %d\n", WSAGetLastError());
closesocket(NewConnection);
WSACleanup();
return;
} printf("We successfully received %d byte(s).\n", Ret); // For this application we do not plan to do anything else with the
// connection so we will just close the connection. printf("We are now going to close the client connection.\n"); closesocket(NewConnection); // When your application is finished handling the connections
// call WSACleanup. WSACleanup();
}
// Module Name: tcpclient.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP client application
// that can send a simple "hello" message to a TCP server listening on port 5150.
// This sample is implemented as a console-style application and simply prints
// status messages a connection is made and when data is sent to the server.
//
// Compile:
//
// cl -o tcpclient tcpclient.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpclient.exe <server IP address>
//#include <winsock2.h>
#include <stdio.h>void main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET s;
SOCKADDR_IN ServerAddr;
int Port = 5150;
int Ret; if (argc <= 1)
{
printf("USAGE: tcpclient <Server IP address>.\n");
return;
} // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to make a client connection.
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
}
// Setup a SOCKADDR_IN structure that will be used to connect
// to a listening server on port 5150. For demonstration
// purposes, we required the user to supply an IP address
// on the command line and we filled this field in with the
// data from the user. ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr(argv[1]); // Make a connection to the server with socket s. printf("We are trying to connect to %s:%d...\n",
inet_ntoa(ServerAddr.sin_addr), htons(ServerAddr.sin_port)); if (connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("connect failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("Our connection succeeded.\n");
// At this point you can start sending or receiving data on
// the socket s. We will just send a hello message to the server. printf("We will now try to send a hello message.\n"); if ((Ret = send(s, "Hello", 5, 0)) == SOCKET_ERROR)
{
printf("send failed with error %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return;
} printf("We successfully sent %d byte(s).\n", Ret); // When you are finished sending and receiving data on socket s,
// you should close the socket. printf("We are closing the connection.\n"); closesocket(s); // When your application is finished handling the connection, call
// WSACleanup. WSACleanup();
}// Module Name: tcpserver.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP server application
// that listens for a TCP connection on port 5150 and receives data. This
// sample is implemented as a console-style application and simply prints
// status messages when a connection is accepted and when data is received
// by the server.
//
// Compile:
//
// cl -o tcpserver tcpserver.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpserver.exe
//
// NOTE: There are no command parameters.
//
#include <winsock2.h>
#include <stdio.h>void main(void)
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
SOCKADDR_IN ClientAddr;
int ClientAddrLen;
int Port = 5150;
int Ret;
char DataBuffer[1024]; // Initialize Winsock version 2.2 if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function. printf("WSAStartup failed with error %d\n", Ret);
return;
}
// Create a new socket to listening for client connections.
if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
printf("socket failed with error %d\n", WSAGetLastError());
WSACleanup();
return;
} // Setup a SOCKADDR_IN structure that will tell bind that we
// want to listen for connections on all interfaces using port
// 5150. Notice how we convert the Port variable from host byte
// order to network byte order. ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Associate the address information with the socket using bind. if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf("bind failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
} // Listen for client connections. We used a backlog of 5 which is
// normal for many applications. if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("listen failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
} printf("We are awaiting a connection on port %d.\n", Port); // Accept a new connection when one arrives. if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error %d\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return;
}
printf("We successfully got a connection from %s:%d.\n",
inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port)); // At this point you can do two things with these sockets. Await
// for more connections by calling accept again on ListeningSocket
// and start sending or receiving data on NewConnection. For
// simplicity We will stop listening for more connections by closing
// ListeningSocket. We will start sending and receiving data on
// NewConnection.
closesocket(ListeningSocket); // Start sending and receiving data on NewConnection. For simplicity,
// we will just receive some data and report how many bytes were
// received. printf("We are waiting to receive data...\n"); if ((Ret = recv(NewConnection, DataBuffer, sizeof(DataBuffer), 0))
== SOCKET_ERROR)
{
printf("recv failed with error %d\n", WSAGetLastError());
closesocket(NewConnection);
WSACleanup();
return;
} printf("We successfully received %d byte(s).\n", Ret); // For this application we do not plan to do anything else with the
// connection so we will just close the connection. printf("We are now going to close the client connection.\n"); closesocket(NewConnection); // When your application is finished handling the connections
// call WSACleanup. WSACleanup();
}
服務程序先在某机器运行比如他的ip地址为192.168.0.xxx
客户端在控制台中打入命令 client.exe 192.168.0.xxx(服務端ip)
如果连接成功服務端会收到hello,并显示接受字节,成功后退出
显示
We are trying to connect to 192.168.0.8:5150...
connect failed with error 10061
我运行tcpserver
显示
We are awaiting a connecting on port 5150
accept failed with error 10014
if ((NewConnection = accept(ListeningSocket, (SOCKADDR *) &ClientAddr,
&ClientAddrLen)) == INVALID_SOCKET)
的问题,我发现你一直没有给ClientAddrLen赋值,请你用sizeof(ClientAddrLen)试一下
accept函数中
客户端这个参数没有初始化,加上就好了
你在程序中寻找一下Port这个字符串
就知道他在什么时候用了端口了