客户端服务器udp mac与windows

时间:2012-12-10 00:00:39

标签: windows macos udp client-server

我将连接到不同平台的电脑: 带窗口的PC我在cygwin和MAC中用gcc编译程序我在终端用gcc编译。

我尝试从网上编程; 当我尝试将窗口作为客户端和mac作为服务器时,Windows可以发送并且mac可以接收。 但是当我尝试将窗口作为服务器和mac作为客户端时,MAc我认为可以发送(因为在终端中有单词表示OK和套接字已关闭)但是窗口无法接收。

这是我的计划: 服务器:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* the port users will be connecting to */
#define MYPORT 4950
#define MAXBUFLEN 500 //13

int main(int argc, char *argv[])
{    int sockfd;
/* my address information */
struct sockaddr_in my_addr;
/* connector’s address information */
struct sockaddr_in their_addr;
int addr_len, numbytes;
char buf[MAXBUFLEN];

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
    perror("Server-socket() sockfd error lol!");
    exit(1);
}
else
    printf("Server-socket() sockfd is OK...\n");

/* host byte order */
my_addr.sin_family = AF_INET;
/* short, network byte order */
my_addr.sin_port = htons(MYPORT);
/* automatically fill with my IP */
my_addr.sin_addr.s_addr = INADDR_ANY;
/* zero the rest of the struct */
memset(&(my_addr.sin_zero), '\0', 8);

if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
    perror("Server-bind() error lol!");
    exit(1);
}
else
    printf("Server-bind() is OK...\n");

addr_len = sizeof(struct sockaddr);

if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1)
{
    perror("Server-recvfrom() error lol!");
    /*If something wrong, just exit lol...*/
    exit(1);
}
else
{
    printf("Server-Waiting and listening...\n");
    printf("Server-recvfrom() is OK...\n");
}

printf("Server-Got packet from %s\n", inet_ntoa(their_addr.sin_addr));
printf("Server-Packet is %d bytes long\n", numbytes);
buf[numbytes] = '\0';
printf("Server-Packet contains \"%s\"\n", buf);

if(close(sockfd) != 0)
    printf("Server-sockfd closing failed!\n");
else
    printf("Server-sockfd successfully closed!\n");
return 0;
 }

在客户端:

 /*senderprog.c - a client, datagram  */
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 /* the port users will be connecting to */
 #define MYPORT 4950

 int main(int argc, char *argv[ ])
 {
int sockfd;
/* connector’s address information */
struct sockaddr_in their_addr;
struct hostent *he;
int numbytes;

if (argc != 3)
{
    fprintf(stderr, "Client-Usage: %s <hostname> <message>\n", argv[0]);
    exit(1);
}
/* get the host info */
if ((he = gethostbyname(argv[1])) == NULL)
{
    perror("Client-gethostbyname() error lol!");
    exit(1);
}
else
    printf("Client-gethostname() is OK...\n");

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
    perror("Client-socket() error lol!");
    exit(1);
}
else
    printf("Client-socket() sockfd is OK...\n");

/* host byte order */
their_addr.sin_family = AF_INET;
/* short, network byte order */
printf("Using port: 4950\n");
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
/* zero the rest of the struct */
memset(&(their_addr.sin_zero), '\0', 8);

if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
{
    perror("Client-sendto() error lol!");
    exit(1);
}
else
    printf("Client-sendto() is OK...\n");

printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));

if (close(sockfd) != 0)
    printf("Client-sockfd closing is failed!\n");
else
    printf("Client-sockfd successfully closed!\n");
return 0;
 }

有人有解决方案吗?

方面 5V

1 个答案:

答案 0 :(得分:1)

我不得不关闭防火墙