As the title says, I am creating a server/client connection. The server will allow multiple clients while the clients can communicate with the server. I have both server and client files. I am not sure where I am going wrong.
I execute each line in cygwin
gcc server.c -o server lpthread
crashes :(
gcc client.c -o client
./client localhost 8080
Server.c
#include <stdio.h>
#include <stdlib.h> // for IOs
#include <string.h>
#include <unistd.h>
#include <sys/types.h> // for system calls
#include <sys/socket.h> // for sockets
#include <netinet/in.h> // for internet
#include <stdbool.h>
#include <pthread.h> // for thread;
/* a function to print out error message and then abort */
void error(const char *msg) {
perror(msg);
exit(1);
}
void *threadFunct(int mySockfd) {
char buffer2[256];
bool exitFlag = false;
int read_writeLen;
while(!exitFlag) {
bzero(buffer2, 256);
read_writeLen = read(mySockfd, buffer2, 255);
if (read_writeLen < 0)
printf("ERROR reading from the client socket\n");
if(strcmp(buffer2,"EXIT\n")==0) {
printf("Now socket %d will be close\n", mySockfd);
close(mySockfd);
pthread_exit(mySockfd); // terminate the calling thread
} else {
printf("The message read from socket %d :%s\n ",mySockfd,buffer2);
read_writeLen = write(mySockfd,"I got Your Message" , 18);
if (read_writeLen < 0)
printf("Unable to write to socket\n");
}
}
close(mySockfd);
return NULL;
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int charRead_Written;
if(argc <2) {
fprintf(stderr, "ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
error("ERROR Opening socket");
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 on binding");
while(true) {
pthread_t threadId;
listen(sockfd,10);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd<0)
error("ERROR on accept");
pthread_create(&threadId,NULL,threadFunct,newsockfd);
}
close(sockfd);
return 0;
}
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdbool.h>
void error(const char *msg) {
perror(msg);
exit(0);
}
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
bool running = false; // Keep running until the user types EXIT
// gcc client.c -o client
// ./client localhost port
char buffer[256];
if (argc <3) {
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd <0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR , no such host \n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0)
error("ERROR Connecting");
while(!running) {
printf("Please Enter The message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
n = write(sockfd, buffer, strlen(buffer));
if(n<0)
error("ERROR writing from socket");
if(strcmp(buffer, "EXIT\n")== 0)
{
running = true;
break;
}
bzero(buffer,256);
n = read(sockfd, buffer,255);
if(n < 0)
printf("ERROR reading from socket");
printf("%s\n", buffer);
}
close(sockfd);
return 0;
}
The client.c file compiles and works fine. The server.c file does not compile and not sure how to solve the issues. Posted compile results below for server.c.
server.c: In function ‘threadFunct’:
server.c:39:17: warning: passing argument 1 of ‘pthread_exit’ makes pointer from integer without a cast [-Wint-conversion]
pthread_exit(mySockfd); // terminate the calling thread
^~~~~~~~
In file included from server.c:18:0:
/usr/include/pthread.h:150:6: note: expected ‘void *’ but argument is of type ‘int’
void pthread_exit (void *) __attribute__ ((__noreturn__));
^~~~~~~~~~~~
server.c: In function ‘main’:
server.c:85:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
pthread_create(&threadId,NULL,threadFunct,newsockfd);
^~~~~~~~~~~
In file included from server.c:18:0:
/usr/include/pthread.h:146:5: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int)’
int pthread_create (pthread_t *, const pthread_attr_t *,
^~~~~~~~~~~~~~
server.c:85:48: warning: passing argument 4 of ‘pthread_create’ makes pointer from integer without a cast [-Wint-conversion]
pthread_create(&threadId,NULL,threadFunct,newsockfd);
^~~~~~~~~
In file included from server.c:18:0:
/usr/include/pthread.h:146:5: note: expected ‘void *’ but argument is of type ‘int’
int pthread_create (pthread_t *, const pthread_attr_t *,
Instead of this:
pthread_exit(mySockfd); // terminate the calling thread
This:
pthread_exit(NULL); // terminate the calling thread
Instead of this:
void *threadFunct(int mySockfd)
This:
struct MyArgs
{
int mySockfd;
};
void *threadFunct(void* args)
{
int mySockfd = ((struct MyArgs*)args)->mySockfd;
Then create the thread as follows:
struct MyArgs* args = (struct MyArgs*)malloc(sizeof(struct MyArgs));
args->mySockfd = mySockfd;
pthread_create(&threadId,NULL,threadFunct,(void*)args);
You're on your own to properly invoke free on that allocate structure after the thread has exited or is about to exit.
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 create two programs that send messages to each other using sockets following the server-client model.
The client is supposed to put a pin (in this case "1234") but the server for some reason skips the function search4pin(). I tried using atoi() to read the variable buffer and storing it to pin but it changed nothing. Using gdb I saw that the variable does not take a value. Thanks for your time.
Server:
#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 <arpa/inet.h>
#include <sys/wait.h>
#define MAX_PINS 1
void error(const char *msg){
perror(msg);
exit(1);
}
int search4pin(int pin,int pins[]){
for(int i=0;i<MAX_PINS;i++){
if(pin==pins[i])
return 0;
}
return 1;
}
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
char buffer[256];
char* comm[20],*cbuff;
struct sockaddr_in serv_addr, cli_addr;
int n,i,done=0,pin,correct=0,pins[MAX_PINS]={1234};
char str[INET_ADDRSTRLEN];
char* args;
if (argc < 2)
{
fprintf(stderr, "No port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, 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 on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
if (inet_ntop(AF_INET, &cli_addr.sin_addr, str, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Could not convert byte to address\n");
exit(1);
}
fprintf(stdout, "The client address is :%s\n", str);
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
sscanf(buffer,"%d",&pin);
while(!search4pin(pin,pins)){
n = write(newsockfd, "Wrong pin!\n", 12);
if (n < 0) error("ERROR writing to socket");
}
/*Some other
code*/
close(newsockfd);
close(sockfd);
return 0;
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg){
perror(msg);
exit(0);
}
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3){
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL){
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("What is the password?\n");
n = write(sockfd, stdin, sizeof(int));
if (n < 0)
error("ERROR writing to socket");
return 0;
}
I want to write a TCP server-client chat, but when I start the two threads for reading from and writing to a socket at both sides, I think they block each other out. Can anyone help me with this?
Server Code:
/* A simple server in the internet domain using TCP the port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
struct server_args_runner{
char buffer [256];
int newsockfd;
pthread_t tid;
pthread_attr_t attr;
//read/write attribute (read == 0 and write == 1)
int rw;
};
void error(char *msg){
perror(msg);
exit(1);
}
void* server_runner_fun(void* args){
// this is the chat part!
// get args:
int n;
struct server_args_runner *sar = (struct server_args_runner*) args;
if(sar->rw == 0){
printf("server thread trys to read from socket...\n");
//read-part
while(1){
bzero(sar->buffer, 256);
n = read(sar->newsockfd, sar->buffer, 255);
if (n < 0){
error("ERROR reading from socket");
}
}
printf("%s\n", sar->buffer);
} else {
printf("server thread trys to write to socket...\n");
//write-part
while(1){
bzero(sar->buffer, 256);
fgets(sar->buffer, 255, stdin);
n = write(sar->newsockfd, sar->buffer, strlen((char *) &(sar->buffer)));
if (n < 0){
error("ERROR writing to socket");
}
}
}
}
int main(int argc, char *argv[]){
//fd = filedescriptor
int sockfd, portno, clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2){
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
//socket(...) returns a descriptor
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1){
error("ERROR opening socket");
}
printf("Socket created successfully.\n");
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;
//htons(..) converts the short from hostbyteorder to networkbyteorder
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) == -1){
error("ERROR on binding");
}
printf("binding successfull on port %d\n", portno);
listen(sockfd, 2);
clilen = sizeof(cli_addr);
printf("server is listening ...\n");
struct server_args_runner server_write_t, server_read_t;
server_write_t.newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
printf("server accepted connection to client.\n");
if (server_write_t.newsockfd < 0){
error("ERROR on accept");
}
//initializing both server_threads
pthread_attr_init(&server_write_t.attr);
pthread_attr_init(&server_read_t.attr);
server_write_t.rw = 1;
server_read_t.rw = 0;
bcopy(&server_write_t.newsockfd, &server_read_t.newsockfd, sizeof(server_write_t.newsockfd));
pthread_create(&server_write_t.tid, &server_write_t.attr, server_runner_fun, &server_write_t);
pthread_create(&server_read_t.tid, &server_read_t.attr, server_runner_fun, &server_read_t);
pthread_join(server_write_t.tid, NULL);
pthread_join(server_read_t.tid, NULL);
return 0;
}
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
struct client_args_runner{
char buffer [256];
int sockfd;
pthread_t tid;
pthread_attr_t attr;
//read/write attribute (read == 0 and write == 1)
int rw;
};
void error(char *msg){
perror(msg);
exit(0);
}
void* client_runner_fun(void* args){
// this is the chat part!
// get args:
int n;
struct client_args_runner *car = (struct client_args_runner*) args;
if(car->rw == 0){
printf("client thread trys to read from socket...\n");
//read-part
while(1){
bzero(car->buffer, 256);
n = read(car->sockfd, car->buffer, 255);
if (n < 0){
error("ERROR reading from socket");
}
}
printf("%s\n", car->buffer);
} else {
printf("client thread trys to write to socket...\n");
//write-part
while(1){
bzero(car->buffer, 256);
fgets(car->buffer, 255, stdin);
n = write(car->sockfd, car->buffer, strlen((char *) &(car->buffer)));
if (n < 0){
error("ERROR writing to socket");
}
}
}
}
int main(int argc, char *argv[]){
int portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
if (argc < 3){
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
struct client_args_runner client_write_t, client_read_t;
client_write_t.sockfd = socket(AF_INET, SOCK_STREAM, 0);
bcopy(&client_write_t.sockfd, &client_read_t.sockfd,
sizeof(client_write_t.sockfd));
if (client_write_t.sockfd == -1){
error("ERROR on creating socket_file_descriptor");
}
printf("socket created successfully.\n");
server = gethostbyname(argv[1]);
printf("hostname is valid.\n");
if(server == NULL){
fprintf(stderr, "Error, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
printf("before connecting to client..\n");
if (connect(client_write_t.sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1){
error("ERROR connecting");
}
printf("client connected successfully to server.\n");
//initializing both client_threads
pthread_attr_init(&client_write_t.attr);
pthread_attr_init(&client_read_t.attr);
client_write_t.rw = 1;
client_read_t.rw = 0;
pthread_create(&client_write_t.tid, &client_write_t.attr, client_runner_fun, &client_write_t);
pthread_create(&client_read_t.tid, &client_read_t.attr, client_runner_fun, &client_read_t);
pthread_join(client_write_t.tid, NULL);
pthread_join(client_read_t.tid, NULL);
return 0;
}
Your printfs in both the client and server readers are outside the while(1) loops, so your client and server are communicating fine, you just aren't printing anything you read from the sockets.
I am building a chat program between a client and a server in C language. Client will connect to server, sends to it a message. Then server will response that meesage.
If there is another client connects to server, the new connection will be created a new thread. So I use pthread.h to build a multi-threaded chat program in C. Please see the server.c and client.c code below to get more details.
server.c
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 7778
void error(char *msg){
perror(msg);
exit(1);
}
void *clientHandler(void*);
int main(){
printf("INFO enter the main()\n");
int sockfd, newsockfd, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, threadID;
pthread_t interrupt;
printf("INFO before calling socket()\n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("INFO after calling socket()\n");
if(sockfd < 0)
error("ERROR opening socket\n");
printf("INFO before calling bzero()\n");
bzero((char*) &serv_addr, sizeof(serv_addr));
printf("INFO after calling socket()\n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
printf("INFO after assigning Internet info\n");
if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding\n");
return 0;
}
printf("INFO before calling listen()\n");
listen(sockfd, 5);
printf("INFO before entering While(1)\n");
while(1)
{
int re;
clilen = sizeof(cli_addr);
printf("INFO before calling accept()\n");
newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
if(newsockfd < 0){
error("ERROR on accepting\n");
return 0;
}
printf("INFO before calling pthread_create()\n");
re = pthread_create(&interrupt, NULL, clientHandler, NULL);
if(re){
printf("ERROR return code from the pthread_create() is %d\n", re);
}
}
printf("INFO before calling pthread_exit(NULL)\n");
pthread_exit(NULL);
return 0;
}
void *clientHandler(void *param){
int n, newsockfd;
newsockfd = *((int*)param);
char buffer[256];
bzero(buffer, 256);
while(1){
n = read(newsockfd, buffer, 255);
if(n < 0){
error("ERROR reading from socket\n");
pthread_exit(NULL);
}
printf("Server received the message: %s", buffer);
n = write(newsockfd, "Server got it\n", 18);
if(n < 0){
error("ERROR writing to socket\n");
pthread_exit(NULL);
}
}
}
client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 7778
#define HOSTNAME "127.0.0.1"
void error(char*msg){
perror(msg);
exit(1);
}
int main(){
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent* server;
//char *hostname = "127.0.0.1";
char buffer[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
error("ERROR opening socket\n");
server = gethostbyname(HOSTNAME);
if(server == NULL){
fprintf(stderr,"ERROR no such host\n");
exit(0);
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(PORT);
if(connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0){
error("ERROR connecting\n");
return 0;
}
while(1)
{
printf("Please enter the message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
n=write(sockfd, buffer, strlen(buffer));
if(n < 0){
error("ERROR reading from socket\n");
return 0;
}
printf("[Server got it] %s\n", buffer);
}
return 0;
}
OK, I builded the *.c files successfully in Linux environment by using terminal.
I used this command line to build server.c
gcc server.c -o server.out -pthread
and use this one to build client.c
gcc client.c -o client.out
Then, I call server.out to run the server:
./server.out
and run client.out
./client.out
BUT, at the time I run server.out, I got the error:
Segmentation fault (core dumped)
Guys, could you share with me your experiences about this. Is my code wrong in somewhere?
This line is passing NULL as the argument of the handler
re = pthread_create(&interrupt, NULL, clientHandler, NULL);
It should be:
re = pthread_create(&interrupt, NULL, clientHandler, &newsockfd);
as Sourav Ghosh comments.
You need to pass newsockt id to the new thread created.
so change
re = pthread_create(&interrupt, NULL, clientHandler, (void*)&newsockfd);
I have added 3rd argument here
typecasting with (void*) will not give you one warning :)
I'm making a simple UDP daytime client. I'm getting this when I try to compile. I've tried casting these to see if they would work but that doesn't lead anywhere. How should I go about entering these parameters with what I have (or am I missing something that should be put there)?
UDPday2.cpp:57:86: error: invalid conversion from ‘int*’ to ‘socklen_t* {aka unsigned int*}’ [-fpermissive]
n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
^
In file included from UDPday2.cpp:6:0:
/usr/include/i386-linux-gnu/sys/socket.h:174:16: error: initializing argument 6 of ‘ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)’ [-fpermissive]
extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
#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 <netdb.h>
#define BUFSIZE 1024
void error(char *msg) {
perror(msg);
exit(0);
}
int main(int argc, char **argv) {
int sockfd, portno, n;
int serverlen;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname;
char buf[BUFSIZE];
portno = 13;
if(argc == 1)
hostname = "localhost";
else
hostname = argv[1];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("socket");
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr," no host %s\n", hostname);
exit(0);
}
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
serverlen = sizeof(serveraddr);
n = sendto(sockfd, buf,(int)strlen(buf)+1, 0,(sockaddr*)&serveraddr , serverlen);
if (n < 0)
error("ERROR send");
n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
if (n < 0)
error("ERROR recvfrom");
printf(" %s", buf);
return 0;
}
serverlen needs to be declared as unsigned, or as a socklen_t. You should have been able to deduce that from the error message.