client/server - Raspberry Pi Forums
i want tcp client/server communication between raspberry pi , laptop ubuntu os running using vmware workstation. raspberry pi act client , ubuntu act server. able compile client , server program.but on client side throws error error, no such host. made changes in ubuntu network settings share other computers. still communication not taking place. able ping between raspberry pi , ubuntu. not able communicate between client , server side. can solve issue , suggest me how network settings in ubuntu pi , ubuntu can communicate.
client.c
client.c
code: select all
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(const char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0) error("error opening socket"); server = gethostbyname(argv[1]); if (server == null) { fprintf(stderr,"error, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = af_inet; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("error connecting"); printf("please enter message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("error writing socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n < 0) error("error reading socket"); printf("%s\n",buffer); close(sockfd); return 0; } server.c /* simple server in internet domain using tcp port number passed argument */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"error, no port provided\n"); exit(1); } sockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0) error("error opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = inaddr_any; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("error on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("error on accept"); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("error reading socket"); printf("here message: %s\n",buffer); n = write(newsockfd,"i got message",18); if (n < 0) error("error writing socket"); close(newsockfd); close(sockfd); return 0; }
do have dns server on local network capable of resolving hostname enter ip address?
or have configured hostname locally in /etc/hosts ?
(the underlying question is: how rpi find out ip address of server?)
or have configured hostname locally in /etc/hosts ?
(the underlying question is: how rpi find out ip address of server?)
raspberrypi
Comments
Post a Comment