1

I am a beginner with sockets in C and trying to develop my first application this is a portion of the client code and it is giving me an error
the error is caused by the line indicated below.

    int sockfd = 0;  

char fromServer[MAXSIZE];

char * fromClient;

fromClient=(char*)malloc(MAXSIZE * sizeof(char));



struct sockaddr_in  remoteServAddr;
    sockfd  = Socket(AF_INET,SOCK_DGRAM,0);

   }
   int n;

   printf(">>");

   scanf("%s",&fromClient); //the error is on this line 

   sendto(sockfd,fromClient,MAXSIZE,0,(SA*)remoteServAddr,sizeof(remoteServAddr));

UPDATE: the error is fixed thank you

5
  • What is the error message? Commented Dec 28, 2013 at 8:21
  • its "cannot convert to a pointer type" Commented Dec 28, 2013 at 8:23
  • Are there any other warnings issued by the conpiler? Commented Dec 28, 2013 at 8:43
  • Fix those and most probably the error also will be gone. Commented Dec 28, 2013 at 9:16
  • 2
    You might like to accept the answer that solved your problem, by clicking the answer's check-mark. Commented Dec 28, 2013 at 9:19

2 Answers 2

3
char * fromClient;

fromClient=(char*)malloc(MAXSIZE * sizeof(char))
/* no need to cast, check the result, and sizeof(char) is always 1 */
...
scanf("%s",&fromClient);
/* scanf expects a pointer and fromClient is already a pointer */

should be

scanf("%s", fromClient);

Anyway, use fgets to prevent buffer overflows:

char *fromClient, *pos;

fgets(fromClient, MAXSIZE, stdin);
if ((pos = strchr(fromClient, '\n')) != NULL)
    *pos = '\0';

EDIT (as suggested by @egur):

And in sendto:

dest_addr: Points to a sockaddr structure containing the destination address. The length and format of the address depend on the address family of the socket.

(SA*)remoteServAddr

should be

(SA*)&remoteServAddr

(assuming that SA is an alias for struct sockaddr)

Sign up to request clarification or add additional context in comments.

2 Comments

@user3108337 Are you sure?
@Alter Mann this is how it looks now ===> char * fromClient; fromClient= malloc(MAXSIZE); fgets(fromClient, MAXSIZE, stdin); yes I am 31:41: error: cannot convert to a pointer type
1
scanf("%s",&fromClient);

-->

scanf("%s", fromClient); // fromClient is already a char*

1 Comment

char * fromClient; fromClient= malloc(MAXSIZE); scanf("%s", fromClient); this still gives the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.