473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

little network/socket-program with parse error

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");
}
Nov 14 '05 #1
7 1516
selekta wrote:
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.

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");
}


Nov 14 '05 #2
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

"selekta" <n.*****@gmx.net> wrote in message
news:c9*************@news.t-online.com...
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");
}

Nov 14 '05 #3
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>"selekta" <n.*****@gmx.net> wrote in message
D>news:c9*************@news.t-online.com...
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>
Nov 14 '05 #4
selekta wrote:
.... 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.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #5
CBFalconer <cb********@yahoo.com> scribbled the following:
selekta wrote:
... 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.
Way to miss the real problem completely, CBFalconer.
int main(void)
{
int sock;
struct sockaddr_in host_addr;

because things like this are undefined here.


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #6
In article <40***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
Thus it is impossible to deal with your problems in this
newsgroup.


And yet, remarkably, several people did.

-- Richard
Nov 14 '05 #7
Richard Tobin wrote:
CBFalconer <cb********@worldnet.att.net> wrote:
Thus it is impossible to deal with your problems in this
newsgroup.


And yet, remarkably, several people did.


More power to them. You concealed it behind a mass of uncheckable
code very well.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
I have a Socket open to a target system. I get the network stream from the open socket and then create a stream reader and stream writer on this stream. The stream reader and writers are operating...
3
by: Michael Rybak | last post by:
Hi, everyone. In topic "2-player game, client and server at localhost", I've asked about subj, and Peter Hansen suggested to switch to Twisted, Pyro or the like. I've tried using Pyro. I've...
0
by: HIL | last post by:
Hi everybody, I wrote a python script to setup my network interface under Linux. To perform it, I use directly from my python script external programs as 'ifconfig' and 'route' ans I fill the...
2
by: Bruce Vander Werf | last post by:
I am developing a network client application (using the Socket class) that will need to make simultaneous TCP connections to many (100 or more) servers. In this case, which would be a better...
5
by: Vijay | last post by:
Hi all, i have TCP client server application written in C# using async socket methods, eg BeginReceive(), BeginEnd() etc server is continuesly running in the background, client connect to...
0
by: Charles Pratt | last post by:
I'm having trouble with a network chat based server I'm working on in C#, It works find during the reads, but as soon as I start sending back data my reads from that point on are corrupted. If...
7
by: simonrigby_uk | last post by:
Hi all, Sorry if this is the incorrect group but I couldn't see anything directly relevant. Can someone confirm for me what happens when two network streams are sent to an application at the...
4
by: Ivan Voras | last post by:
I have a simple network protocol client (it's a part of this: http://sqlcached.sourceforge.net) implemented in Python, PHP and C. Everything's fine, except that the Python implementation is the...
4
by: dumbkiwi | last post by:
I have written a script that uses the urllib2 module to download web pages for parsing. If there is no network interface, urllib2 hangs for a very long time before it raises an exception. I...
13
by: 7stud | last post by:
I have the following two identical clients #test1.py:----------- import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = 'localhost' port = 5052 #server port
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.