×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

string assignment

string assignment

string assignment

(OP)
Hello,

I am doing some socket programming and have created a library.

CODE

int CreateSocket(char* domain, char* type, int protocal, unsigned int portNumber, char* IPAddress)
{
    struct sockaddr_in serverAddress;
    int sockfd = 0;
    int connectionResult = 0;

    char family[10];

    strcpy(family, domain);
    printf("family = %s", family);
    printf("Char* domain = %s\n", domain);
    printf("(int) domain = %d\n", (int) domain);
    printf("(int) *domain = %d\n", (int) *domain);

    serverAddress.sin_family = *domain; //PROBLEM HERE ASSIGNING THE FAMILY DOMAIN.
     serverAddress.sin_port = htons(portNumber);
     serverAddress.sin_addr.s_addr = inet_addr(IPAddress);

    memset(serverAddress.sin_zero, '\0', sizeof(serverAddress.sin_zero));
    
    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    printf("socket(PF_INET, SOCK_STREAM) = %d", sockfd);

    if(sockfd == -1)
    {
        WriteErrorLog(1);
        return -1;
    }
    .
        .
        .


I have above function that will pass the address family (AF_INET) with some other parameters. The parameter name is domain.

I think the problem is that the AF_INET is a key word so cannot assign a string representative. As you can see from my print outs I have tried many things.

I need this parameter passed in as the user will either enter the address family or protocol family that they want to use. This will also be the same for the SOCK_STREAM or SOCK_DGRAM.

Many thanks for any advice,

Steve

RE: string assignment

(OP)
Hello,

I have just discovered that this works below:

serverAddress.sin_family = (int) *domain;

This gives the value of 65 that is assigned to the sin_family.

I guess the 65 is the value that is assigned to the AF_INET. Correct if I am wrong.

Steve

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close