ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int connectFd, listenFd, portn, clientlen, nbyte, i;
char buffer[256];
struct sockaddr_in addrserv, addrclient;
int cltlen;
if (argc < 2) printf("Devi inserire il numero di porta");
listenFd = socket(AF_INET, SOCK_STREAM, 0);
printf("listenFd = %d ", listenFd);
if (listenFd < 0)
perror("Errore nella apertura del socket");
bzero(&addrserv, sizeof(addrserv));
portn = atoi(argv[1]);
addrserv.sin_family = AF_INET;
addrserv.sin_addr.s_addr = htonl(INADDR_ANY);
addrserv.sin_port = htons(portn);
if (bind(listenFd, (struct sockaddr*)&addrserv, sizeof(addrserv)) < 0)
perror("Errore nella operazione di bind del socket");
if (listen(listenFd, 5) < 0)
perror("Errore nella operazione di listen del socket");
cltlen = sizeof(addrclient);
On success, accept() returns a non-negative integer that is a descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.
0 seems a good value. You should change the test to:
Code:
if (connectFd = accept(listenFd, (struct sockaddr*)&addrclient, (socklen_t*)&cltlen) < 0) {
And please, when you put code, use the code tag. (it is in the editor...)
Another problem is that you need to handle errors a bit better. In this example, I simply terminate the program with a "perror()" and a "return 1" if anything goes wrong.
Also note the "code tags" vladmihaisima mentioned:
Code:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char*argv[])
{
int connectFd, listenFd, portn, clientlen, nbyte, total_bytes, i;
char buffer[256];
struct sockaddr_in addrserv, addrclient;
int cltlen;
if (argc < 2)
{
printf("Devi inserire il numero di porta");
return 1;
}
listenFd = socket(AF_INET, SOCK_STREAM, 0);
if (listenFd < 0)
{
perror("Errore nella apertura del socket");
return 1;
}
printf("listenFd = %d ", listenFd);
bzero(&addrserv, sizeof(addrserv));
portn = atoi(argv[1]);
addrserv.sin_family = AF_INET;
addrserv.sin_addr.s_addr = htonl(INADDR_ANY);
addrserv.sin_port = htons(portn);
if (bind(listenFd, (struct sockaddr*)&addrserv, sizeof(addrserv)) < 0)
{
perror("Errore nella operazione di bind del socket");
return 1;
}
if (listen(listenFd, 5) < 0)
{
perror("Errore nella operazione di listen del socket");
return 1;
}
cltlen = sizeof(addrclient);
if (connectFd = accept(listenFd, (struct sockaddr*)&addrclient, (socklen_t*)&cltlen) < 0)
{
perror("Errore nella operazione di accept ");
return 1;
}
printf("ConnectFd = %d ", connectFd);
bzero(buffer, 256);
if (nbyte = recv(connectFd, buffer, 255, 0) < 0)
{
printf("Codice errore: %d", errno);
perror("Errore nella ricezione");
}
printf("Byte ricevuti: %d", nbyte);
shutdown(connectFd, SHUT_RDWR);
close(connectFd);
close(listenFd);
printf ("Ciao!\n");
return 0;
}
Also - are you sure the client actually *sends* a byte?
Please be sure to put the same kind of error checking into your client as we did above, in your server.
Also: consider a tool like Wireshark (formerly "Ethereal") to study the bytes being sent and received over the specified port:
Without having read your client code (I might have, if you'd taken the advice of at least two others, and posted it in [CODE] tags), I always advocate for using a known good client or server for the peer node when developing networking applications. As a client, telnet is usually a good choice. As a server, I favor netcat (nc). Doing this allows you to focus your efforts on only one end of the system, getting it working without having to consider which side of the system is causing a failure. Sometimes, a very simple client or server implementation in a scripting language such as Perl can be an asset, due to the easy parsing or disassembly of received data, and fast turnaround of modifications.
--- rod.
if (sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0) {
= has a lower precedence than <, so if the socket call succeeds, sockfd gets the result of the comparison which is 1 (true). You should write it like this:
Code:
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
or even
Code:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
If you compile with -Wall gcc will tell about this ("suggest parentheses around assignment used as truth value").
the following code sections contain you original source with some markup from me. Most of the markup is in the server.c file. The code compiles, and works as designed. Although you know you should make several improvements in error checking and flow control. Let me know what more you need. Again, here is a link with more detailed examples and discussion. http://www.beej.us/guide/bgnet/outpu...l#simpleserver
Code:
/* client.c
* gcc -Wall -o client client.c
* usage: ./client 127.0.0.1 6000
*/
#include <stdio.h>
#include <stdlib.h> /* for atoi() */
#include <unistd.h> /* for close() */
#include <string.h> /* for strerror(), bzero() */
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc, char* argv[])
{
int sockfd, nbyte;
struct sockaddr_in srv_addr;
char buffer[256];
if (argc != 3) {
printf("Command syntax \n");
printf(" server <IP ADDRESS> <PORT NUMBER> \n");
}
else {
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error in socket()");
}
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons(atoi(argv[2]));
srv_addr.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(sockfd, (struct sockaddr*)&srv_addr, sizeof(srv_addr)) < 0) {
perror("Error in connect()");
return 1;
}
printf("Inserrt message to send");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
if ((nbyte = send(sockfd, buffer, strlen(buffer), 0)) < 0) {
perror("Error in send()");
}
printf("Sent bytes: %d ", nbyte);
if (nbyte < 0)
perror("Errore nella scrittura su socket");
bzero(buffer, 255);
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
return 0;
}
return 1;
}
Code:
/* server.c
* gcc -Wall -o server server.c
* usage: ./server 6000
*/
#include <stdio.h>
#include <stdlib.h> /* for atoi() */
#include <unistd.h> /* for close() */
#include <string.h> /* for strerror() */
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc, char* argv[])
{
int sockfd, new_fd;
struct sockaddr_in srv_addr, clt_addr;
int clt_size, nbyte;
char buffer[256];
if (argc != 2) {
printf("Command syntax \n");
printf(" server <PORT NUMBER> \n");
}
else {
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error in socket() %d, %s", errno, strerror(errno)); /* added some extra info */
return 1; /* no need to continue */
}
clt_addr.sin_family = AF_INET;
/* clt_addr.sin_port = htons(argv[1]); needs to be an int, argv[1] is a string now] */
clt_addr.sin_port = htons(atoi(argv[1]));
/* clt_addr.sin_addr.s_addr = htonl(INADDR_ANY); INADDR_ANY is already in the right format */
clt_addr.sin_addr.s_addr = INADDR_ANY;
memset(srv_addr.sin_zero, '\0', sizeof(srv_addr.sin_zero));
if (bind(sockfd, (struct sockaddr*)&clt_addr, sizeof(srv_addr)) < 0) {
printf("Error in bind(): %d, %s", errno, strerror(errno));
close (sockfd);
return 1;
}
if (listen(sockfd, 5) < 0) {
printf("Error in listen(): %d, %s", errno, strerror(errno));
close (sockfd);
return 1;
}
clt_size = sizeof(clt_addr);
if ( (new_fd = accept(sockfd, (struct sockaddr*)&clt_addr, (socklen_t *)&clt_size)) < 0) { /* cast to (socklen_t *)&clt_size */
printf("Error in accept(): %d, %s", errno, strerror(errno));
close (sockfd);
return 1;
}
bzero(buffer, 256);
if ( (nbyte = recv(new_fd, buffer, 255, 0)) < 0) { /* more () to clean up compile warning */
printf("Error in recv(): %d, %s", errno, strerror(errno));
close (new_fd);
close (sockfd);
return 1;
}
printf("Received bytes: %d", nbyte);
shutdown(new_fd, SHUT_RDWR);
close(new_fd);
return 0;
}
return 1;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.