当前位置: 代码迷 >> VC >> 互访有关问题(server与client互机可以通信)
  详细解决方案

互访有关问题(server与client互机可以通信)

热度:6657   发布时间:2013-02-25 00:00:00.0
互访问题(server与client互机可以通信)
请问如何实现client/server模式的互通信问题啊,我实现不了啊
我是刚开始学习vc.net所以不要大家笑话我啊……
server 端:
SOCKET UDPserver(const char *service);

void main()
{
char *service ="3334";
struct sockaddr_in sin;
int socket;
WSADATA wsadata;
int alen,cc;
char buf[BUFSIZE];

if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize error!\n");
WSACleanup();
exit(1);
}

socket=UDPserver(service);

while(1)
{
alen=sizeof(struct sockaddr);
if((cc=recvfrom(socket,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)
{
buf[cc]='\0';
printf("%s",buf);
}
}

closesocket(socket);
exit(0);
}
client 端:
SOCKET UDPconnect(const char *,const char *);
void UDPtest(char *,char *);

#define LINELEN 128
#define WAVERS MAKEWORD(2,0)

void main()
{
char *host="localhost";
char *service="3334";
WSADATA wsadata;

if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize error!\n");
WSACleanup();
exit(1);
}

UDPtest(host,service);
  WSACleanup();
  exit(0);
}

void UDPtest(char *host,char *service)
{
char buf[LINELEN+1];
SOCKET s;
int outchars;

s=UDPconnect(host,service);
while(fgets(buf,sizeof(buf),stdin))
{
buf[LINELEN]='\0';
outchars=strlen(buf);
send(s,buf,outchars,0);

if(buf[0]=='\n')
break;
}
  closesocket(s);
  exit(0);  
}
请大家帮忙改一下了。先谢谢了


------解决方案--------------------------------------------------------
你的UDPServer是怎么写的?

------解决方案--------------------------------------------------------
C/C++ code
// test8.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include  <stdlib.h> #include  <stdio.h> #include  <string.h> #include  <winsock.h> SOCKET sockserver(const char *service,const char *transport,int qlen) {     //struct servent *servin;     //struct protoent *protoin;     struct sockaddr_in ipaddr;     int    sock,type;     memset(&ipaddr,0,sizeof(ipaddr));     ipaddr.sin_family=AF_INET;     ipaddr.sin_addr.s_addr= htonl( INADDR_ANY );     /*    //俺手头没有msdn,这段函数看不懂    if(servin=getservbyname(service,transport))     {        ipaddr.sin_port=htons(ntohs((u_short)servin->s_port));     }    else if((ipaddr.sin_port=htons((u_short)atoi(service)))==0)     {         printf("get port number error!\n");         WSACleanup();         exit(1);     }     if((protoin=getprotobyname(transport))==0)     {         printf("get protocol information error!\n");         WSACleanup();         exit(1);     }     */    ipaddr.sin_port = htons( 9999 );        //使用端口9999    if(strcmp(transport,"udp")==0)         type=SOCK_DGRAM;     else         type=SOCK_STREAM;     //先改成能够使用的方式    //sock=socket(AF_INET,type,protoin->p_proto);     sock=socket(AF_INET,type,IPPROTO_UDP);     if(INVALID_SOCKET == sock)     {         printf("creat socket error!\n");         WSACleanup();         exit(1);     }     if(bind(sock,(struct sockaddr *)&ipaddr,sizeof(ipaddr))==SOCKET_ERROR)     {         printf("socket bind error!\n");         WSACleanup();         exit(1);     }     if(type==SOCK_STREAM)     {         if(listen(sock,qlen)==SOCKET_ERROR)         {             printf("socket listen error!\n");             WSACleanup();             exit(1);         }     }     return sock; } SOCKET UDPserver(const char *service) {     return sockserver(service,"udp",0); } SOCKET TCPserver(const char *service,int qlen) {     return sockserver(service,"tcp",qlen); } #define        BUFSIZE            (1024)void main() {     char *service ="3334";     struct sockaddr_in sin;     SOCKET sc;     WSADATA wsadata;     int alen,cc;     char buf[BUFSIZE];     WORD WAVERS = MAKEWORD( 2, 0 );    if(WSAStartup(WAVERS,&wsadata)!=0)     {         printf("initalize error!\n");         WSACleanup();         exit(1);     }     sc=UDPserver(service);     while(1)     {         alen=sizeof(struct sockaddr);         if((cc=recvfrom(sc,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)         {             buf[cc]='\0';             printf("%s",buf);         }     }     closesocket(sc);     exit(0); }
  相关解决方案