I write a network.h and network.c file. When I compile it with gcc. It show a strange
error.
gcc -o network.o -c network.h
network.c:14: error: expected ‘;’, ‘,’ or ‘)’ before ‘{’ token
The code try to send message through socket.
This is the header file.
network.h
#ifndef _NETWORK_H_
#define _NETWORK_H_
int open_tcp(char* host, int port);
int nsend(int sock_fd, char* buffer, int num);
int nrecv(int sock_fd, char* buffer, int num;
int recv_line();
int close(int socket_fd);
#endif
This is the part of implements.
network.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include "network.h"
#define BUFFER_SIZE 300000
int open_tcp(char* host, int port)
{
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = -1;
char buffer[BUFFER_SIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(host);
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,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
return sockfd;
}
Thank you for your help.
If you give a minute to read the error it says,
Now, if you know that
#includejust includes the file and put the content of the file into the current file, so you can just count the lines (in this case as the files are small), you’ll arrive at line 6 innetwork.h, which has the issue i.e. this lineHope this helps in your better understanding and help you resolves such error by finding the exact error location.