Cześć,
próbuję napisać (z pomocą tutoriali, mana i innych źródeł) prostą implementację chatu. Niestety zatrzymałem się już na samym wstępie. Na początek wklejam kody serwera i klienta:
Kod serwera
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <netinet/in.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
#include <string.h>
#define MYPORT 3490
int main(int argc, char *argv){
struct sockaddr_in my_addr;
struct sockaddr_in cli_addr;
int sockfd;
int newsockfd;
socklen_t clilen;
int n;
char client_message[2000];
//socket declaration
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//filling struct
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(& (my_addr.sin_zero), '0', 8);
//binding socket to address and port
if(bind(sockfd, (struct sockaddr *)& my_addr, sizeof(my_addr))<0)
printf("Bind error");
//listening on the socket
listen(sockfd, 5);
printf("Waiting for connection...\n");
clilen = sizeof(cli_addr);
//accepting connection
if (newsockfd = accept(sockfd, (struct sockaddr *)& cli_addr, &clilen) < 0){
printf("Unable to connect\n");
}
else
{
printf("Connected with %s\n", inet_ntoa(cli_addr.sin_addr));
}
while( (n = recv(newsockfd , client_message , 2000 , 0)) > 0 )
{
printf(client_message);
}
if(n == 0)
{
printf("Client disconnected");
}
else if(n == -1)
{
printf("recv failed");
}
}
Kod klienta
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <netinet/in.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <stdio.h> /* for perror() */
#include <stdlib.h> /* for exit() */
#include <string>
int main(int argc, char * argv[]){
int n;
int client;
int dest_port = 3490;
char buffer[1000];
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket created\n");
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(dest_port);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero),'0',8);
if(connect(client, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)
printf("Connection error");
printf("Connected with server\n");
while(1)
{
printf("Enter message : ");
scanf("%s" , buffer);
//Send some data
if( send(client , buffer , strlen(buffer) , 0) < 0)
{
printf("Send failed");
return 1;
}
}
}
Po skompilowaniu i uruchomieniu najpierw serwera a potem klienta, serwer zachowuje się w ten sposób:
Waiting for connection...
Connected with 127.0.0.1
recv failed
i kończy pracę.
Natomiast klient czeka na wpisanie wiadomości.
Czy ktoś mógłby mi wytłumyczyć lub spróbować nakierować co robię źle? Dlaczego serwer nie otrzymuje prawidłowo wiadomości z klienta i nawet nie czeka na jej wysłanie?