Prosty jednokierunkowy chat z wykorzystaniem gniazd (socket)

0

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?

0

I co tu nie działa?

0

Wybacz pingwindykator, edytowałem post. Mógłbyś zerknąć teraz? Dziękuję.

0

Klient łączy się z netcatem i wszystko w miarę wygląda ok.
Serwer, tutaj zmienona wersja:


#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define PORT 3490
#define BUF_LEN 2000

int main(int argc, char **argv){
	int sockfd, newsockfd, n;
	socklen_t clilen;
	char *MSG;
	struct sockaddr_in serv_addr, cli_addr;

	//Create socket
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0){
		perror("SOCKET");
		exit(1);
	}

	//zero memory in structure
	memset((char *) &serv_addr,0, sizeof(serv_addr));
	//passing arguments to structure
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(PORT);

	//binding socket
	if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
		perror("BINDING");
		exit(1);
	}

	//listen
	listen(sockfd,5);
	clilen = sizeof(cli_addr);
	//accept 
	newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
	if (newsockfd < 0){
		perror("ACCEPT");
		exit(1);
	}

	//alloc memory
	MSG = (char *)malloc(BUF_LEN);

	while(1){
		n = recv(newsockfd,MSG,BUF_LEN-1,0);
		if (n < 0){
			perror("RECV");
			close(newsockfd);
			close(sockfd);
			free(MSG);
			exit(1);
		}else if(n == 0){
			printf("client %s close connection\n",(char *)inet_ntoa(cli_addr.sin_addr));
			free(MSG);
			break;
		}
		MSG[n] = 0;
		printf("Msg from %s: %s\n",(char *)inet_ntoa(cli_addr.sin_addr),MSG);
	}
	return 0; 
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1