C Server

It has been a while since I have done any programming because I have been finishing up my math classes, and I seem to forgotten a lot when it comes to C. I am having the hardest time figuring out why my code is not working.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SERVER_H
#define SERVER_H

#define SERVER_IPV4 11
#define SERVER_IPV6 12

typedef struct server server;

int server_init(server *serv, int port, int  communication_type);

int server_start_listen(server *serv, int backlog);

int server_stop_listen(server *serv);

int server_send(server *serv, void *data);

void * server_recieve(server *serv);

void server_destory(server *serv);

#endif /* server.h */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "server.h"
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>

struct server {
	int port; 
	int type;
};

int server_init(server *serv, int port, int communication_type)
{
	serv = (server *)malloc(sizeof(server));
	if(serv) {
		serv->port = port;
		serv->type = (communication_type != SERVER_IPV4) ? SERVER_IPV6 : SERVER_IPV4;
		printf("type: in the init function %i\n", communication_type);
		printf("server->type in the init function: %i\n", serv->type);
	} else {
		return -1;
	}
	return 0;
}

int server_start_listen(server *serv, int backlog)
{
	printf("starting the server on port %i\n", serv->port);
	return 0;
}

int server_stop_listen(server *serv)
{
	printf("stopping the server\n");
	return 0;
}

int server_send(server *serv, void *data)
{
	printf("server type as an integer in the send function: %i\n", serv->type);
	printf("server port  as an integer in the send function: %i\n", serv->port);
	return 0;
}

void * server_recieve(server *serv)
{
	printf("the server is getting data\n");
	return NULL;
}

void server_destroy(server *serv)
{
	if(serv)
		free(serv);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include "server.h"

int main(int argc, char **argv)
{
	server *server; 
	server_init(server, 2021, 12);
	server_start_listen(server, 5);


	char *buffer = "this is a sentence";
	server_send(server, (void *)buffer);

	return 0;
}


This is a very simple program. I plan to accomplish more with it, but I want to get the functions working correctly before I expand its functionality. I am not sure why my program is outputting different integers for its type and port number.

I want server to be an opaque object, so later on I can just write server_listen(&server);
Here is the output of the program:

1
2
3
4
5
type: in the init function 12
server->type in the init function: 12
starting the server on port 1
server type as an integer in the send function: 0
server port  as an integer in the send function: 1


Thanks for all the help.


EDIT:
I discovered that the malloc is causing the issue.
serv = (server *)malloc(sizeof(server)); I assume that I am referencing two different objects, but how do I accomplish this by allocating space for server on the heap? If you remove the malloc code everything works, but it is not what I want because server is allocated on the stack.

Edit #2:
I changed the server_init function to the following
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server * server_init(int port, int communication_type)
{
	server *serv = (server *)malloc(sizeof(server));
	printf("the address of server after mallocing %p\n", serv);

	if(serv) {
		serv->port = port;
		serv->type = (communication_type != SERVER_IPV4) ? SERVER_IPV6 : SERVER_IPV4;

		printf("type: in the init function %i\n", communication_type);

	} else {
		NULL;
	}

	printf("server->type in the init function: %i\n", serv->type);
	return serv;
}


If you have any suggestions or any improvements please let me know
Last edited on
Your initial version is passing the pointer (server *) by value.

I believe it will work if you pass a pointer to the pointer:
In main:server_init(&server, 2021, 12);
In server.cpp:
1
2
3
4
5
int server_init(server **serv, int port, int communication_type)
{
	*serv = (server *)malloc(sizeof(server));
	if(*serv) {
           // and so on 
Last edited on
Worked like a charm. Thanks for the help.
Topic archived. No new replies allowed.