Witam.
Mam problem, a mianowicie mam kod serwera do komunikatora internetowego i chcę żeby funkcje wysyłające i odbierające wiadomości działały współbieżnie. Poniżej wstawiam kod przed przerobieniem i po przerobieniu, Proszę o pomoc! Projekt mam do przedstawienia na jutro, a czasu coraz mniej :/.
Kod kompiluje na Ubuntu w gcc. Jeśli trafi się ktoś pomocny prosiłbym o odpowiedź czy ma to w ogóle prawo działać. Rzecz w tym żeby te funkcje send i read działały współbieżnie, bo chcę żeby wiadomości od klienta wyświetlały się od razu w terminalu a nie dopiero gdy odpisze (wcisnę enter).
Przed przerobieniem:
#include <arpa/inet.h>
#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[])
{
char message[255];
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 nie podano portu\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR gniazda");
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 binda");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR akceptowania");
printf("Polaczono z klientem %s port %d CTRL+C Zakoncz konwersacje\n",
inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
while(1){
printf("Twoja wiadomosc:");
fgets(message,255,stdin);
send(newsockfd, message , strlen(message), 0);
printf("\n");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR odczytu ");
printf("Klient: %s",buffer);
printf("\n");
}
close(newsockfd);
close(sockfd);
return 0;
}
Po:
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <thread>
char message[255];
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
void obslugawysylania()
{
printf("Twoja wiadomosc:");
fgets(message,255,stdin);
send(newsockfd, message , strlen(message), 0);
printf("\n");
bzero(buffer,256);
}
void obslugaodbierania()
{
n = read(newsockfd,buffer,255);
if (n < 0) perror("ERROR odczytu ");
printf("Klient: %s",buffer);
printf("\n");
}
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr,"ERROR nie podano portu\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR gniazda");
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 bind");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR akceptowania");
printf("Polaczono z klientem %s port %d CTRL+C Zakoncz konwersacje\n",
inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
while(1){
std ::thread jeden(&obslugaodbierania);
std ::thread dwa(&obslugawysylania);
}
close(newsockfd);
close(sockfd);
return 0;
}