2012-12-10 229 views
0

我將連接到不同平臺的PC: 帶窗口的PC我在cygwin和MAC中使用gcc編譯程序我使用gcc在終端編譯。客戶端服務器udp mac與windows

我嘗試從網絡上的程序; 當我嘗試窗口作爲客戶端和Mac作爲服務器,Windows可以發送和MAC可以接收。 但是當我嘗試window作爲服務器和mac作爲客戶端時,我認爲可以發送(因爲終端中有OK和socket關閉的詞)但窗口無法接收。

這裏是我的程序: 服務器:

#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

+0

我解決了這個問題,我是傻不關閉火線 – Limavolt

+0

可以請你指導我,我的問題的http://計算器。 COM /問題/ 42500940 /如何對廣播話筒音頻通過-UDP網絡 –

回答