little network/socket-program with parse error

S

selekta

hi,

i've recently started to deal a little bit with tcp/ip-socket-programming
(linux).
Now i've written a little test-program (as displayed further down).
When i'm trying to compile it the gcc-compiler return "parse error in
l.23/.24 before token '='. "
the line before those two ( host_addr.sin_family = AF_INET;) is acepted
without any problems.
I'd be happy if anybody could tell me where's my fault.

Thanks a lot.

I'm sorry for mistakes in my english.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define host_IP = "127.0.0.1"; //IP only for test purpose
#define host_PORT = 1024; //port number only for test purpose

int main(void)
{
int sock;
struct sockaddr_in host_addr;

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
printf("couldn't establish socket.\n");
else
printf("socket created.\n");

host_addr.sin_family = AF_INET;
// l. 23// host_addr.sin_port = htons(host_PORT);
//l. 24// host_addr.sin_addr.s_addr = inet_addr(host_IP);

if (connect(sock, (struct sockaddr *)&host_addr,sizeof(&host_addr)) != -1)
printf("connected to server.\n");
else
printf("couldn't connect.\n");

close(sock);
printf("socket closed.\n");
}
 
D

Devrobcom

Your defines shall not have any =
#define host_IP "127.0.0.1"; //IP only for test purpose
#define host_PORT 1024; //port number only for test purpose
 
C

Case

selekta said:
hi,

i've recently started to deal a little bit with tcp/ip-socket-programming
(linux).
Now i've written a little test-program (as displayed further down).
When i'm trying to compile it the gcc-compiler return "parse error in
l.23/.24 before token '='. "
the line before those two ( host_addr.sin_family = AF_INET;) is acepted
without any problems.
I'd be happy if anybody could tell me where's my fault.

Thanks a lot.

I'm sorry for mistakes in my english.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define host_IP = "127.0.0.1"; //IP only for test purpose
#define host_PORT = 1024; //port number only for test purpose

The preprocessor directive #define should be seen as nothing
more than a text-processor 'find-replace' operation. So what
happens below where you get compiler errors is that

'host_PORT'

is literally replaced with

'= 1024;'

Looking at your code below, clearly this is an error. So:

#define host_PORT 1024

BTW, it is a kind of non-written rule to write #define'd names
in capitals: 'HOST_PORT'.

Same for 'host_IP'.

Many compiler have a switch to spit out the code after preprocessing.
In a few well known compilers this is the -E switch. Can be handy
in cases like this.
 
H

Harti Brandt

On Tue, 1 Jun 2004, Devrobcom wrote:

D>Your defines shall not have any =
D>#define host_IP "127.0.0.1"; //IP only for test purpose
D>#define host_PORT 1024; //port number only for test purpose

They also should not have a ';'.

harti

D>
D>D>> hi,
D>>
D>> i've recently started to deal a little bit with tcp/ip-socket-programming
D>> (linux).
D>> Now i've written a little test-program (as displayed further down).
D>> When i'm trying to compile it the gcc-compiler return "parse error in
D>> l.23/.24 before token '='. "
D>> the line before those two ( host_addr.sin_family = AF_INET;) is acepted
D>> without any problems.
D>> I'd be happy if anybody could tell me where's my fault.
D>>
D>> Thanks a lot.
D>>
D>> I'm sorry for mistakes in my english.
D>>
D>> #include <stdio.h>
D>> #include <sys/types.h>
D>> #include <sys/socket.h>
D>> #include <netinet/in.h>
D>> #include <arpa/inet.h>
D>> #include <unistd.h>
D>>
D>> #define host_IP = "127.0.0.1"; //IP only for test purpose
D>> #define host_PORT = 1024; //port number only for test purpose
D>>
D>> int main(void)
D>> {
D>> int sock;
D>> struct sockaddr_in host_addr;
D>>
D>> sock = socket(AF_INET, SOCK_STREAM, 0);
D>> if (sock == -1)
D>> printf("couldn't establish socket.\n");
D>> else
D>> printf("socket created.\n");
D>>
D>> host_addr.sin_family = AF_INET;
D>> // l. 23// host_addr.sin_port = htons(host_PORT);
D>> //l. 24// host_addr.sin_addr.s_addr = inet_addr(host_IP);
D>>
D>> if (connect(sock, (struct sockaddr *)&host_addr,sizeof(&host_addr))
D>!= -1)
D>> printf("connected to server.\n");
D>> else
D>> printf("couldn't connect.\n");
D>>
D>> close(sock);
D>> printf("socket closed.\n");
D>> }
D>>
D>>
D>
D>
D>
 
C

CBFalconer

selekta said:
.... snip ...

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

None of the above, apart from stdio.h, are defined in standard C.
Thus it is impossible to deal with your problems in this
newsgroup. You need a system specific group.
#define host_IP = "127.0.0.1"; //IP only for test purpose
#define host_PORT = 1024; //port number only for test purpose

Do not use // comments in newsgroups, they do not survive line
wrapping particularly well.
int main(void)
{
int sock;
struct sockaddr_in host_addr;

because things like this are undefined here.
 
J

Joona I Palaste

None of the above, apart from stdio.h, are defined in standard C.
Thus it is impossible to deal with your problems in this
newsgroup. You need a system specific group.
Do not use // comments in newsgroups, they do not survive line
wrapping particularly well.

Way to miss the real problem completely, CBFalconer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top