I would like to make program so that, when client disconnected, the server will back ready to accept a new request from client. Here is the code
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXLINE 100
#define LISTENQ 10
int main()
{
int tmp, listenfd, connfd;
int sin_size;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;
while(true)
{
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(13);
memset(&(servaddr.sin_zero),'\0',8);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd == -1){
perror("error socket");
exit(1);
}
tmp=bind(listenfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr));
if(tmp == -1){
perror("error bind..");
exit(1);
}
tmp=listen(listenfd, LISTENQ);
if(tmp == -1){
perror("error listen");
exit(1);
}
sin_size = sizeof(struct sockaddr_in);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr, &sin_size);
if(connfd == -1){
perror("error accept");
exit(1);
}
ticks = time(NULL);
snprintf(buff,sizeof(buff),"Now Time: %.24s\r\n", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
close(listenfd);
}
}
I found a problem on this part
tmp=bind(listenfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr));
error: invalid conversion from 'int*' to 'socklen_t* {aka unsigned int*}' [-fpermissive]
connfd=accept(listenfd,(struct sockaddr *)&cliaddr, &sin_size);
error: initializing argument 3 of 'int accept(int, sockaddr*, socklen_t*)' [-fpermissive]
What should I do with those problem? Didn't the pointer right?
You need to use
socklen_t sin_size;
Chnage your declaration
socklen_t sin_size;
This solves your issue.
you can also Try this as last resort but above change would works fine for you.
Modify this statement
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,(socklen_t * ) &sin_size);
See man accept
After seeing #alk comment i am adding this part from comment.
Especially when casting pointers, it might just cast away an compile-time error but very well might crash when running as the sizes of what the program expects when Dereferencing the pointer differs from reality.
Use while(1) instead of while(true) and use a C-compiler instead of a C++-compiler.
The errors come from compiling the source code with a C++compiler. If you use a C-Compiler your code does not produce the mentioned errors, except that now true is not defined.
Correct answer, need to run on root-access user
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#define MAXLINE 100
#define LISTENQ 10
int main()
{
int tmp, listenfd, connfd;
socklen_t sin_size;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;
while(true)
{
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(13);
memset(&(servaddr.sin_zero),'\0',8);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd == -1){
perror("error socket");
exit(1);
}
tmp=bind(listenfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr));
if(tmp == -1){
perror("error bind..");
exit(1);
}
tmp=listen(listenfd, LISTENQ);
if(tmp == -1){
perror("error listen");
exit(1);
}
sin_size = sizeof(struct sockaddr_in);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr, &sin_size);
if(connfd == -1){
perror("error accept");
exit(1);
}
ticks = time(NULL);
snprintf(buff,sizeof(buff),"Sekarang pukul: %.24s\r\n", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
close(listenfd);
}
}
Related
I am trying to compile this C program but I am getting this message
warning: passing 'int *' to parameter of type 'socklen_t *'
(aka 'unsigned int *') converts between pointers to integer types with
different sign [-Wpointer-sign]
if ((new_s = accept(s, (struct sockaddr *) &sin, &addr_len)) < 0) {
^~~~~~~~~
someone could help me?
#include<string.h>
#include<stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
int main()
{
struct sockaddr_in sin;
char buf[MAX_LINE];
int buf_len, addr_len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
while(1) {
if ((new_s = accept(s, (struct sockaddr *) &sin, &addr_len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while (buf_len == recv(new_s, buf, sizeof(buf), 0))
fputs(buf, stdout);
close(new_s);
}
}
As the error message states, you're passing an int * argument to accept for the last argument when the function expects a socklen_t * for this argument.
So change the type of addr_len to socklen_t.
I am Trying to make a C/C++ Project using Socket Programming.
For this I need Client and Server to sent a index of array back and forth to each other.
So Client will next a index of array and server will sent another index of array until the game is over. (Yeah we are trying to make a game.)
I have written (copied from gfg) code for it but it's not working properly.
The Server for some reason didn't receive the second message. So, We kind of stuck in a deadlock.
Any idea what I did wrong??
Server.cpp
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *msg = "0";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
int i=0;
while(i<10)
{
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , msg , strlen(msg) , 0 );
printf("Message sent\n");
i++;
}
return 0;
}
Client.cpp
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *msg="0";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
int i=0;
while(i<10)
{
send(sock , msg , strlen(msg) , 0 );
printf("Message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
i++;
}
return 0;
}
Edit: It will be really nice if somebody can tell me how to sent an integer instead of a string.
I'm kind of new to sockets. So I setup a server and I want to connect a client to it through the internet. I don't understand what I'm doing wrong because the following example works with the loopback address (127.0.0.1) but not when someone else tries to connect to my server using my external IP address. Is it just not that simple or am I doing something wrong in my code?
Edit: (The client specifically gets stuck when it reaches connect() giving a connect: Connection timed out error)
Edit2: I tried using my broadcast address inet_addr("192.168.1.255") instead of INADDR_ANY in the server code. No change.
This is my server code:
#include <stdio.h>
#include <stdlib.h\
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(void) {
int temp;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int numbytes;
char buf[100];
int sock_fd; // server file descriptor
int new_fd; // client file descriptor
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd == -1) { perror("socket"); exit(1); }
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(9000);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
temp = bind(sock_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("bind"); exit(1); }
temp = listen(sock_fd, 5);
if (temp == -1) { perror("listen"); exit(1); }
printf("\nListening for new connections on port %d ...\n\n", 9000);
while (1) {
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sock_fd, (struct sockaddr*)&their_addr, &sin_size);
if (new_fd == -1) { perror("accept"); continue; }
printf("Got connection from IP (%s)\n", inet_ntoa(their_addr.sin_addr));
close(new_fd);
}
}
This is my client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc, char *argv[]) {
int temp;
struct hostent *host_info;
struct sockaddr_in serv_addr;
char ip_addr[100];
int numbytes;
char buf[100];
int sock_fd;
inet_pton(AF_INET, argv[5], &serv_addr.sin_addr);
host_info = gethostbyaddr(&serv_addr.sin_addr, sizeof serv_addr.sin_addr, AF_INET);
if (host_info == NULL) { herror("gethostbyname"); exit(1); }
printf("\nServer: '%s' (IP: %s , Port: %d)\n\n", argv[1], inet_ntop(AF_INET, host_info->h_addr, ip_addr, 100), 9000);
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd == -1) { perror("socket"); exit(1); }
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
serv_addr.sin_addr = *((struct in_addr *)host_info->h_addr);
bzero(&(serv_addr.sin_zero), 8);
temp = connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("connect"); exit(1); }
close(sock_fd);
exit(0);
}
Posting an answer here so I can accept my overall solution. The problem was that I had not forwarded a port on my router as #AdamRosenfield indicated, then after I did that I changed my server's address to:
my_addr.sin_addr.s_addr = inet_addr("192.168.1.2");
which is MY local IP address according to ifconfig (I am using Ubuntu).
Now my only problem is that anyone else can connect to my server except me from my own pc, but that might be a problem specific to my router according to #nos.
If anyone has an answer on how to fix that please update me (I tried connecting a client using the loopback address, my external IP, my local IP and a few more, nothing worked).
the client.c should be as below
int main(int argc, char *argv[]){
int temp;
struct hostent *host_info;
struct sockaddr_in serv_addr;
char ip_addr[100];
int numbytes;
char buf[100];
int sock_fd;
unsigned long inaddr;
//inet_pton(AF_INET, argv[5], &serv_addr.sin_addr);
//host_info = gethostbyaddr(&serv_addr.sin_addr, sizeof serv_addr.sin_addr, AF_INET);
//if (host_info == NULL) { herror("gethostbyname"); exit(1); }
printf("\nServer: '%s' (IP: %s , Port: %d)\n\n", argv[0], argv[1], 9000);
inaddr = inet_addr(argv[1]);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) { perror("socket"); exit(1); }
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
memcpy(&serv_addr.sin_addr, &inaddr, sizeof(inaddr));
//bzero(&(serv_addr.sin_zero), 8);
temp = connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("connect"); exit(1); }
close(sock_fd);
exit(0);
}
then use g++ compile it:g++ client.c -o client.
use client like this:./client yourserverIP
when set the server address in client code, you can do like this:
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//your server's ip address
I am trying to implement a tcp client and tcp server. I am able to establish the connection but when I send a message from the client, the server doesn't receive it and yes I did look at the previous posts and there were alot of similar problems. I did follow them but I am still getting the same error. The error i am getting is from server side:
recv: Socket operation on non-socket
Here is my code. If you can please let me know what I am doing wrong, I would really appreciate it. I think there is a problem in my server implementation.
Server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 3490
#define BACKLOG 10
int main()
{
struct sockaddr_in server;
struct sockaddr_in dest;
int status,socket_fd, client_fd,num;
socklen_t size;
char buffer[10240];
memset(buffer,0,sizeof(buffer));
int yes = 1;
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket failure!!\n");
exit(1);
}
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
memset(&server, 0, sizeof(server));
memset(&dest,0,sizeof(dest));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1) { //sizeof(struct sockaddr)
fprintf(stderr, "Binding Failure\n");
exit(1);
}
if ((listen(socket_fd, BACKLOG))== -1){
fprintf(stderr, "Listening Failure\n");
exit(1);
}
while(1) {
size = sizeof(struct sockaddr_in);
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size)==-1)) {
//fprintf(stderr,"Accept Failure\n");
perror("accept");
exit(1);
}
printf("Server got connection from client %s\n", inet_ntoa(dest.sin_addr));
//buffer = "Hello World!! I am networking!!\n";
if ((num = recv(client_fd, buffer, 10239,0))== -1) {
//fprintf(stderr,"Error in receiving message!!\n");
perror("recv");
exit(1);
}
// num = recv(client_fd, buffer, sizeof(buffer),0);
buffer[num] = '\0';
printf("Message received: %s\n", buffer);
close(client_fd);
return 0;
//close(socket_fd);
}
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 3490
int main(int argc, char *argv[])
{
struct sockaddr_in server_info;
struct hostent *he;
int socket_fd,num;
char *buffer;
if (argc != 2) {
fprintf(stderr, "Usage: client hostname\n");
exit(1);
}
if ((he = gethostbyname(argv[1]))==NULL) {
fprintf(stderr, "Cannot get host name\n");
exit(1);
}
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
fprintf(stderr, "Socket Failure!!\n");
exit(1);
}
memset(&server_info, 0, sizeof(server_info));
server_info.sin_family = AF_INET;
server_info.sin_port = htons(PORT);
server_info.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(socket_fd, (struct sockaddr *)&server_info, sizeof(struct sockaddr))<0) {
//fprintf(stderr, "Connection Failure\n");
perror("connect");
exit(1);
}
buffer = "Hello World!! I am networking!!\n";
if ((send(socket_fd,buffer, sizeof(buffer),0))== -1) {
fprintf(stderr, "Failure Sending Message\n");
close(socket_fd);
exit(1);
}
else {
printf("Message being sent: %s\n",buffer);
}
close(socket_fd);
}
I ran the server under gdb, and discovered that client_fd is 0 after the call to accept(). This is an invalid socket fd, so I looked at that line of code and noticed that the closing parenthesis is wrong:
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size)==-1)) {
should be:
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1) {
Otherwise, it's doing the comparison first and then assigning the comparison to client_fd, whereas you want the assignment of the socket, followed by the comparison.
To avoid this exact kind of frustrating bug, it's generally considered best practice to not put assignments inside of 'if' statements. I would recommend instead:
client_fd = accept(...);
if (client_fd < 0) { ... }
Also, in the client, the call to send() uses "sizeof(buffer)". 'buffer' is a char*, and the sizeof a pointer is 4 (on a 32-bit system), so only 'Hell' will be sent. To send the full string, use "strlen(buffer)" instead for the amount to send.
Your first problem is misplaced parentheses.
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size)==-1)) {
should actually be
if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1) {
As you currently have it, client_fd will be assigned to the result of the equality test between the return value of accept() and -1 and thus will always be zero in case of success.
This is one reason why many programmers avoid assignments in if statements. If written like this
client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size);
if (client_fd == -1) {
then the error can't occur.
Any help/feedback would be appreciated. i would like to set a listener (server) such that it would receive data from a broadcaster client and then it would send information over the same socket. Is this possible to do in C Programming if so how?
EDIT: CODE ADDED
/* udpserver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int addr_len, bytes_read;
char recv_data[1024];
struct sockaddr_in server_addr , client_addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
printf("\nUDPServer Waiting for client on port 5000");
fflush(stdout);
while (1)
{
bytes_read = recvfrom(sock,recv_data,1024,0,
(struct sockaddr *)&client_addr, &addr_len);
recv_data[bytes_read] = '\0';
printf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port));
printf("%s", recv_data);
fflush(stdout);
/* HERE IS MY CODE ADDED */
if (sendto(sockfd, "HELLO", 5, 0, (struct sockaddr *)&client_addr, &addr_len) == -1) {
perror("talker: sendto");
exit(1);
}
}
return 0;
}
Sockets are already bidirectional, meaning you can send and receive data with the same connection.
There is a good example of a TCP/UDP server and client in C here
Is the recvFrom working? Do you get the prints which you have after recvFrom()?