M
Michal J
I don't know what I'm doing wrong ??
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
struct irc_server
{
//glowne dane
char *hostname;
int port;
char *username;
//kanaly
int number_of_channels;
char *channels[IRC_MAX_CHANNELS];
//socket
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
};
int irc_init_server(struct irc_server *server, char *username, char
*hostname, int port)
{
//alokowanie pamieci glownej struktory
server = (struct irc_server*)malloc(sizeof(struct irc_server));
//wpisujemy podstawowe informacje
server->hostname = (char*)malloc(sizeof(char)*strlen(hostname));
strcpy(server->hostname, hostname);
server->username = (char*)malloc(sizeof(char)*strlen(username));
strcpy(server->username, username);
server->port=port;
//socket
if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
{
return 1;
}
//reszte informacji o serververze
server->server = gethostbyname(server->hostname);
if(server->server == NULL)
{
return 2;
}
server->serv_addr.sin_family = AF_INET;
server->serv_addr.sin_port = htons(server->port);
server->serv_addr.sin_addr = *((struct in_addr
*)server->server->h_addr);
memset(&(server->serv_addr.sin_zero), '\0', 8);
// printf("hostname %s\n", server->hostname);
return 0;
}
main()
{
struct irc_server* server;
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak",
"www.wp.pl", 7666));
if(server == NULL)
{
printf("null\n");
}
else
{
printf("not null> %d\n", server->hostname); //and I get
Segmentation fault.
}
}
what is wrong??
PS. THX
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
struct irc_server
{
//glowne dane
char *hostname;
int port;
char *username;
//kanaly
int number_of_channels;
char *channels[IRC_MAX_CHANNELS];
//socket
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
};
int irc_init_server(struct irc_server *server, char *username, char
*hostname, int port)
{
//alokowanie pamieci glownej struktory
server = (struct irc_server*)malloc(sizeof(struct irc_server));
//wpisujemy podstawowe informacje
server->hostname = (char*)malloc(sizeof(char)*strlen(hostname));
strcpy(server->hostname, hostname);
server->username = (char*)malloc(sizeof(char)*strlen(username));
strcpy(server->username, username);
server->port=port;
//socket
if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
{
return 1;
}
//reszte informacji o serververze
server->server = gethostbyname(server->hostname);
if(server->server == NULL)
{
return 2;
}
server->serv_addr.sin_family = AF_INET;
server->serv_addr.sin_port = htons(server->port);
server->serv_addr.sin_addr = *((struct in_addr
*)server->server->h_addr);
memset(&(server->serv_addr.sin_zero), '\0', 8);
// printf("hostname %s\n", server->hostname);
return 0;
}
main()
{
struct irc_server* server;
printf("start\n");
printf("initcode: %d\n", irc_init_server(server, "bak",
"www.wp.pl", 7666));
if(server == NULL)
{
printf("null\n");
}
else
{
printf("not null> %d\n", server->hostname); //and I get
Segmentation fault.
}
}
what is wrong??
PS. THX