Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am trying to make chat for two users(using select()), but i am stuck on this code:
<pre>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


void chat(int sock_fd, struct sockaddr_in *peer){
  
  int ret;
  ssize_t bytes;
  char buffer[1024];
  char sendto_buffer[1024];
  fd_set readfds;
  FD_ZERO(&readfds);
  FD_SET(STDIN_FILENO, &readfds);
  FD_SET(sock_fd, &readfds);

  while (1) {
    select(sock_fd + 1, &readfds, NULL, NULL, NULL);
    
  
      if (FD_ISSET(STDIN_FILENO, &readfds)) {
        bytes = read(0, sendto_buffer, sizeof(sendto_buffer));
        if (bytes < 0) {
          printf("Error\n");
          break;
        }
        printf("Sending: %.*s\n", (int)bytes, sendto_buffer);
        bytes = sendto(sock_fd, sendto_buffer, bytes, 0,(struct sockaddr *)peer, sizeof(struct sockaddr_in));
        if (bytes < 0) {
          printf("Error sendto \n");
          break;
        }
      }
    
      if(FD_ISSET(sock_fd, &readfds)) {
        bytes = recvfrom(sock_fd, buffer, sizeof(buffer), 0, NULL, NULL);
        if (bytes < 0) {
          printf("Error recvfrom\n");
          break;
        }
        if (bytes > 0) {
          printf("Received: %.*s\n", (int)bytes, buffer);
        }
      }
    }
  }


int main(int argc, char *argv[]){
  
  int localport;
  int remoteport;
  int sock_fd;
  struct sockaddr_in server_addr;
  struct sockaddr_in peer_addr;

 if (argc < 4) {
    printf("Usage %s <local port> <remote host> <remote port>\n", argv[0]);
    return 1;
  }
  localport = strtoul(argv[1], NULL, 0);
  emoteport = strtoul(argv[3], NULL, 0);

  peer_addr.sin_family = AF_INET;
  peer_addr.sin_port = htons(remoteport);
  peer_addr.sin_addr.s_addr = INADDR_ANY;

  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if(sock_fd == -1){
    perror("Socket error ");
    return 1;
  }

  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(localport);
  int local_bind = bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
    if(local_bind == -1){
      perror("Bind error ");
      return 1;
    }
  chat(sock_fd, &peer_addr);

  close(sock_fd);

  return 0;
}

Can someone give me some advice?
Thanks

What I have tried:

I tried to use select for this, but I think that I miss something important.
Posted
Updated 19-Jun-21 1:57am
v2

1 solution

As I told you yesterday you are referring to remoteportand localport, but you never initialise them to anything. Look at your main code:
C++
int main(int argc, char *argv[]){
  
  int localport;
  int remoteport;
  int sock_fd;
  struct sockaddr_in server_addr;
  struct sockaddr_in peer_addr;

 if (argc < 4) {
    printf("Usage %s <local port> <remote host> <remote port>\n", argv[0]);
    return 1;
  }

// what is missing here? You have no code to transfer the parameters in the argv array, to
// the relevant variables in your code. So everything that happens from here will be wrong.
   
Comments
Member 15245946 19-Jun-21 6:37am
   
I initialise them:
localport = strtoul(argv[1], NULL, 0);
remoteport = strtoul(argv[3], NULL, 0);
But something is still wrong, Now it is working: one person is sender and second one is receiver..
Richard MacCutchan 19-Jun-21 7:51am
   
Not in the code that you have posted in your question you don't.
Richard MacCutchan 19-Jun-21 8:06am
   
In yesterday's question you stated that you start it by
./chat 127.0.0.1 1345 1346

Which does not match the order you use in your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900