这个c/s连天程序 为什么只能在本机上通信,而在两台之间就连不了了?
服务器端:#include<windows.h>#include<stdio.h>DWORDWINAPIFunProc(LPVOIDlpParameter);voidma...
服务器端:
#include <windows.h>
#include <stdio.h>
DWORD WINAPI FunProc(LPVOID lpParameter);
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendbuf[100];
sprintf(sendbuf,"Welcome %s to c/s chating room.",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendbuf,strlen(sendbuf)+1,0);
recv(sockConn,sendbuf,100,0);
printf("%s\n",sendbuf);
int i=1;
while(i)
{
char sendbuf[100];
char recvbuf[100];
recv(sockConn,recvbuf,100,0);
if('q'==recvbuf[0])
{
send(sockSrv,"q",strlen("q")+1,0);
printf("Chat end!");
i=0;
}
printf("Client says:\n");
printf("%s\n",recvbuf);
printf("you say:");
gets(sendbuf);
send(sockConn,sendbuf,strlen(sendbuf)+1,0);
}
closesocket(sockConn);
}
}
客户端:
#include <windows.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {return;}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {WSACleanup( );return; }
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("172.17.216.113");//我的ip(校园网的,在我电脑运行服务器端。)
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvbuf[100];
recv(sockClient,recvbuf,100,0);
printf("%s\n",recvbuf);
send(sockClient,"连接成功!",strlen("连接成功!")+1,0);
int i=1;
while(i)
{
char sendbuf[100];
printf("you say:");
gets(sendbuf);
send(sockClient,sendbuf,strlen(sendbuf)+1,0);
char recvbuf[100];
recv(sockClient,recvbuf,100,0);
if('q'==recvbuf[0])
{
send(sockClient,"q",strlen("q")+1,0);
printf("Chat end!");
i=0;
}
printf("Server says:\n");
printf("%s\n",recvbuf);
}
closesocket(sockClient);
WSACleanup();
}
在本机上一切正常,就是可以发送接收字符(一发一接),而当客户端与服务器端分别在不同的机子上运行时,就不能连接了。这是什么情况?
请高手指点!! 展开
#include <windows.h>
#include <stdio.h>
DWORD WINAPI FunProc(LPVOID lpParameter);
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendbuf[100];
sprintf(sendbuf,"Welcome %s to c/s chating room.",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendbuf,strlen(sendbuf)+1,0);
recv(sockConn,sendbuf,100,0);
printf("%s\n",sendbuf);
int i=1;
while(i)
{
char sendbuf[100];
char recvbuf[100];
recv(sockConn,recvbuf,100,0);
if('q'==recvbuf[0])
{
send(sockSrv,"q",strlen("q")+1,0);
printf("Chat end!");
i=0;
}
printf("Client says:\n");
printf("%s\n",recvbuf);
printf("you say:");
gets(sendbuf);
send(sockConn,sendbuf,strlen(sendbuf)+1,0);
}
closesocket(sockConn);
}
}
客户端:
#include <windows.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {return;}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {WSACleanup( );return; }
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("172.17.216.113");//我的ip(校园网的,在我电脑运行服务器端。)
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvbuf[100];
recv(sockClient,recvbuf,100,0);
printf("%s\n",recvbuf);
send(sockClient,"连接成功!",strlen("连接成功!")+1,0);
int i=1;
while(i)
{
char sendbuf[100];
printf("you say:");
gets(sendbuf);
send(sockClient,sendbuf,strlen(sendbuf)+1,0);
char recvbuf[100];
recv(sockClient,recvbuf,100,0);
if('q'==recvbuf[0])
{
send(sockClient,"q",strlen("q")+1,0);
printf("Chat end!");
i=0;
}
printf("Server says:\n");
printf("%s\n",recvbuf);
}
closesocket(sockClient);
WSACleanup();
}
在本机上一切正常,就是可以发送接收字符(一发一接),而当客户端与服务器端分别在不同的机子上运行时,就不能连接了。这是什么情况?
请高手指点!! 展开
光派通信
2024-09-03 广告
2024-09-03 广告
对光传输设备进行调试和维护,需要掌握以下关键点:1. **熟悉设备性能**:深入了解OTN/DWDM等光传输产品的性能特点,确保能够熟练使用设备。2. **按规范操作**:依据产品说明书和维护手册进行调试和维护,确保操作步骤正确无误。3. ...
点击进入详情页
本回答由光派通信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询