[英]C socket programming under windows
Hi I'm new to socket programming and I'm trying out the following code from the tutorial of http://www.binarytides.com/winsock-socket-programming-tutorial/ 嗨,我是套接字编程的新手,我正在尝试http://www.binarytides.com/winsock-socket-programming-tutorial/的教程中的以下代码
I'm trying to connect to server and I'm using the IP address of google. 我正在尝试连接到服务器,并且正在使用google的IP地址。 Here is the code: 这是代码:
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
return 0;
}
So far socket can be created but I can't connect to the server. 到目前为止,可以创建套接字,但是我无法连接到服务器。 To be more specific I always exit and have following: 更具体地说,我总是退出并具有以下内容:
The program '[2060] SocketCTest.exe: Native' has exited with code 1 (0x1). 程序“ [2060] SocketCTest.exe:本机”已退出,代码为1(0x1)。
even if I set a breakpoint before returns. 即使我在返回前设置了断点。
There's no error. 没有错 I've tried on my computer and connect returned with error ( WSAETIMEDOUT
). 我已经在计算机上尝试过,并且连接返回错误( WSAETIMEDOUT
)。 I'm not sure whether there are the network settings on my computer, proxies, firewalls, and so on; 我不确定计算机,代理服务器,防火墙等上是否有网络设置; or that google host is configured not to accept direct socket connections. 或该Google主机配置为不接受直接套接字连接。
Anyway here's your code with some small adjustments: 无论如何,这是您的代码,进行了一些小的调整:
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc, char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char c = 0;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
printf("Failed. Error Code : %d.\nPress a key to exit...", WSAGetLastError());
c = getch();
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Could not create socket : %d.\n", WSAGetLastError());
WSACleanup();
c = getch();
return 1;
}
printf("Socket created. Connecting...\n");
memset(&server, 0, sizeof server);
server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect error:%d.\nPress a key to exit...", WSAGetLastError());
closesocket(s);
WSACleanup();
c = getch();
return 1;
}
puts("Connected.\nPress a key to exit...");
closesocket(s);
WSACleanup();
c = getch();
return 0;
}
Now if you want to see the code actually connecting i'd suggest using localhost
( 127.0.0.1
) instead of 74.125.224.72
, and the port 3389
for example (it's the Remote Desktop Server (RDP) port, if you have RDP configured and running on your computer) instead of 80
; 现在,如果您想查看实际连接的代码,建议您使用localhost
( 127.0.0.1
)而不是74.125.224.72
和端口3389
(例如,如果配置了RDP且是远程桌面服务器(RDP)端口,在您的计算机上运行)而不是80
; or you could let 80
if you have a web server ( IIS ?) running on your computer. 如果您的计算机上运行的是Web服务器( IIS ?),则可以设置为80
。
To get a list of server programs that run on your computer run the command: 要获取在计算机上运行的服务器程序的列表,请运行以下命令:
`netstat -an | findstr LISTEN`
which would output a bunch of lines (and the ones that we care about are) in this form (here's the one corresponding to the RDP example from above): 它将以这种形式输出一堆线(以及我们关心的线)(这是与上面的RDP示例相对应的线):
`TCP 127.0.0.1:3389 0.0.0.0:0 LISTENING`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.