为什么这两个程序在本机上可连接,外网就不行呢?错误10060
#include <winsock.h>#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
int main()
{
int c,d,e,f,g,k,n,z,i,p,q;
char s[1000];
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
SOCKET a,b;
g = WSAStartup( wVersionRequested, &wsaData );
if ( g != 0 ) {
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
return(0);
}
struct sockaddr_in remote_addr,my_addr;
a=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
if(a==-1)
{
printf("创建SOCKET失败,错误码 %d",GetLastError());
}
memset(&remote_addr,0,sizeof(remote_addr));
remote_addr.sin_family=AF_INET;//远程主机使用协议//
remote_addr.sin_port=htons(888);//远程主机端口//
remote_addr.sin_addr.S_un.S_addr = inet_addr("172.17.36.25");//远程主机IP//
printf("客户端已启动!\n");
printf("正在连接...\n");
c=connect(a,(struct sockaddr*)&remote_addr,sizeof(remote_addr));//向服务器发出连接请求//
if(c==-1)
{
printf("连接失败! %d\n",GetLastError());
exit(1);
}
else
{
printf("连接成功!\n");
}
while(c!=-1)//循环发送接收//
{
recv(a,s,sizeof(s),0);//接收客户断发送的数据,并显示//
printf("他说: %s\n\n",s);
printf("我说:" "");
scanf("%s",&s);
printf("发送中..\t");
q=send(a,s,sizeof(s),0);//发送信息//
if(q!=-1)
{
printf("发送成功!\n\n");
}
else
{
printf("发送失败!\n");
return(0);
}
}
closesocket(a);//关闭socket//
WSACleanup();//释放占用资源//
}
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 1, 1 );
int err;
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we couldn't find a useable */
/* winsock.dll. */
return(0);
}
int c,d,e,g,i,n,q,z,k,p;
SOCKET a,b;
char s[1000];
struct sockaddr_in my_addr;
n=strlen(s);
struct sockaddr_in remote_addr;
a=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
if(a==-1)
{
printf("创建SOCKET失败",NULL,MB_OK);
}
memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;//本地主机使用协议//
my_addr.sin_port=htons(888);//本地主机端口//
my_addr.sin_addr.S_un.S_addr = INADDR_ANY;//本机IP//
if(bind(a,( struct sockaddr*)&my_addr,sizeof(my_addr))==-1)
{
printf("bind错误! 错误码%d\n",GetLastError());
return(0);
}
c=listen(a,10);//调用listen()函数监听端口//
if(c==-1)
{
printf("端口监听失败! 错误码%d\n",GetLastError());//出错提示//
return(0);
}
else
{
printf("服务器已启动!\n");
}
d=sizeof(remote_addr);
printf("等待连接...\n");
b=accept(a,(struct sockaddr*)&remote_addr,&d);//accept()函数,用于接受客户端的连接请求//
if(b!=-1)
{
printf("已连接上!\n");
}
while(b!=-1)
{
printf("我说: " "");
scanf("%s",&s);//输入数据,并发送//
printf("发送中...\t");
p=send(b,s,sizeof(s),0);
if(p!=-1)
{
printf("发送成功!\n\n");
}
else
{
printf("发送失败!\n");
return(0);
}
recv(b,s,sizeof(s),0);//发送完毕,准执行此函数准备接收数据//
printf("他说:" "%s\n\n",s);//显示收到的数据//
}
WSACleanup();//释放资源//
closesocket(a);//关闭socket//
closesocket(b);//关闭socket1//
}