robertos18 Temat założony przez niniejszego użytkownika |
Problem z portem » 2019-01-26 17:52:32 Witam. Muszę przerobić ten kod tak, żeby mozna było pisać z jednego portu (chat UDP) Otwierając dwa terminale (loguje sie na 2 roznych maszynach) Na 1 terminalu: ./chat 5555 (IP drugiej maszyny do ktorej bede wysylac odpowiedzi) 5556 Na 2 terminalu: ./chat 5556 (IP pierwszej maszyny do ktorej bede wysylac odpowiedzi) 5555 // gdzie 5555 i 5556 to porty a o to kod: #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <errno.h> #include <fcntl.h> #include <netinet/in.h> #include <poll.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
void start_chat( int sock_fd, struct sockaddr_in * peer ) { int ret; ssize_t bytes; char input_buffer[ 1024 ]; char output_buffer[ 1024 ]; struct pollfd fds[ 2 ]; fds[ 0 ].fd = 0; fds[ 1 ].fd = sock_fd; fds[ 0 ].events = POLLIN | POLLPRI; fds[ 1 ].events = POLLIN | POLLPRI; while( 1 ) { ret = poll( fds, 2, - 1 ); if( ret < 0 ) { printf( "Error - poll zwrocil error: %s\n", strerror( errno ) ); break; } if( ret > 0 ) { if( fds[ 0 ].revents &( POLLERR | POLLHUP | POLLNVAL ) ) { printf( "Error - stdin error\n" ); break; } if( fds[ 1 ].revents &( POLLERR | POLLHUP | POLLNVAL ) ) { printf( "Error - socket error\n" ); break; } if( fds[ 0 ].revents &( POLLIN | POLLPRI ) ) { bytes = read( 0, output_buffer, sizeof( output_buffer ) ); if( bytes < 0 ) { printf( "Error - stdin error: %s\n", strerror( errno ) ); break; } printf( "Sending: %.*s\n",( int ) bytes, output_buffer ); bytes = sendto( sock_fd, output_buffer, bytes, 0, ( struct sockaddr * ) peer, sizeof( struct sockaddr_in ) ); if( bytes < 0 ) { printf( "Error - sendto error: %s\n", strerror( errno ) ); break; } } if( fds[ 1 ].revents &( POLLIN | POLLPRI ) ) { bytes = recvfrom( sock_fd, input_buffer, sizeof( input_buffer ), 0, NULL, NULL ); if( bytes < 0 ) { printf( "Error - recvfrom error: %s\n", strerror( errno ) ); break; } if( bytes > 0 ) { printf( "Received: %.*s\n",( int ) bytes, input_buffer ); } } } } }
int main( int argc, char * argv[] ) { unsigned long local_port; unsigned long remote_port; 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; } local_port = strtoul( argv[ 1 ], NULL, 0 ); if( local_port < 1 || local_port > 65535 ) { printf( "Error - invalid local port '%s'\n", argv[ 1 ] ); return 1; } remote_port = strtoul( argv[ 3 ], NULL, 0 ); if( remote_port < 1 || remote_port > 65535 ) { printf( "Error - invalid remote port '%s'\n", argv[ 3 ] ); return 1; } peer_addr.sin_family = AF_INET; peer_addr.sin_port = htons( remote_port ); if( inet_aton( argv[ 2 ], & peer_addr.sin_addr ) == 0 ) { printf( "Error - invalid remote address '%s'\n", argv[ 2 ] ); return 1; } sock_fd = socket( AF_INET, SOCK_DGRAM, 0 ); if( sock_fd < 0 ) { printf( "Error - failed to open socket: %s\n", strerror( errno ) ); return 1; } server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl( INADDR_ANY ); server_addr.sin_port = htons( local_port ); if( bind( sock_fd,( struct sockaddr * )( & server_addr ), sizeof( server_addr ) ) < 0 ) { printf( "Error - failed to bind socket: %s\n", strerror( errno ) ); return 1; } start_chat( sock_fd, & peer_addr ); close( sock_fd ); return 0; }
|
|
pekfos |
» 2019-01-26 17:56:26 A masz problem z..? |
|
robertos18 Temat założony przez niniejszego użytkownika |
» 2019-01-27 11:12:17 Nie wiem jak to przerobic zeby mozna bylo na 1 porcie pisac. Potrzebuje wskazówek |
|
johndevid4 |
port » 2019-01-29 06:00:17 << removed - spam >> |
|
pekfos |
» 2019-01-29 23:22:56 Co się dzieje, gdy podasz ten sam port dla local i remote? |
|
« 1 » |