newbie help with sockets programming... 
Author Message
 newbie help with sockets programming...

Hi,

I wondered if someone can help me... I have this code which connects to a
socket on the net (http server) and retreived the output of a HEAD / to the
server. Although ive made a mistake. Could someone help me with this ;)

$ cat sockfork.c:
#include "sockfork.h"

int main()
{
 /* --- define variables --- */
 int sock,result;
 int connected = 1;
 char request[24] = "HEAD / HTTP/1.0\r\n\r\n";
 char response[1024];

 // create the socket
 sock = socket(AF_INET,SOCK_STREAM,0);
 if(sock < 0)
  // socket creation failed
  perror("socket()");
  exit(1);
 else
  // socket created
  printf("Created socket `sock\'\n");

 printf("Attemting to connect to `%s\' on port
`%d\'\n",REMOTE_SERV,TCP_PORT);

 result = connect(sock,REMOTE_SERV,TCP_PORT);
 if(result < 0)
  perror("connect()");
  exit(1);
 else
  printf("Got result back from `%s\' on port `%d\'\n",REMOTE_SERV,TCP_PORT);

 printf("Sending HTTP HEAD request to `%s\' on port
`%d\'\n",REMOTE_SERV,TCP_PORT);
 send(sock,request,strlen(request));
 printf("Sent HTTP HEAD request to `%s\' on port
`%d\'\n",REMOTE_SERV,TCP_PORT);

 printf("Reading response from `%s\' on port `%d\'\n",REMOTE_SERV,TCP_PORT);
 while(response = recv(sock,1024))
  printf("%s",response);

 printf("\nClosing socket `sock\'...");
 shutdown(sock);
 printf("closed\n");

 return(0);
 exit(0);

Quote:
}

$ cat sockfork.h
/* --- include headers --- */
#include <stdio.h>
#include <sys/socket.h>

/* --- macro's --- */
#define REMOTE_SERV "www.redhat.com"
#define TCP_PORT    80

When i try and compile it, e.g. gcc -o sockfork sockfork.c i get an error
with connect about pointers :-( i haven't quite made my way to pointers...
If someone could help that would be great.

Cheers,

Elfyn



Mon, 04 Apr 2005 12:19:05 GMT  
 newbie help with sockets programming...

Quote:
> Hi,
> I wondered if someone can help me... I have this code which connects to a
> socket on the net (http server) and retreived the output of a HEAD / to the
> server. Although ive made a mistake. Could someone help me with this ;)

Sockets are OS-specific and thus off-topic for comp.lang.c.

Quote:
> $ cat sockfork.c:
> #include "sockfork.h"
> int main()
> {
>  /* --- define variables --- */
>  int sock,result;
>  int connected = 1;
>  char request[24] = "HEAD / HTTP/1.0\r\n\r\n";
>  char response[1024];
>  // create the socket
>  sock = socket(AF_INET,SOCK_STREAM,0);

Non-standard function call.

Quote:
>  if(sock < 0)
>   // socket creation failed
>   perror("socket()");
>   exit(1);
>  else
>   // socket created
>   printf("Created socket `sock\'\n");
>  printf("Attemting to connect to `%s\' on port
> `%d\'\n",REMOTE_SERV,TCP_PORT);
>  result = connect(sock,REMOTE_SERV,TCP_PORT);

Non-standard function call.

Quote:
>  if(result < 0)
>   perror("connect()");
>   exit(1);
>  else
>   printf("Got result back from `%s\' on port `%d\'\n",REMOTE_SERV,TCP_PORT);
>  printf("Sending HTTP HEAD request to `%s\' on port
> `%d\'\n",REMOTE_SERV,TCP_PORT);
>  send(sock,request,strlen(request));

Non-standard function call.

Quote:
>  printf("Sent HTTP HEAD request to `%s\' on port
> `%d\'\n",REMOTE_SERV,TCP_PORT);
>  printf("Reading response from `%s\' on port `%d\'\n",REMOTE_SERV,TCP_PORT);
>  while(response = recv(sock,1024))

Non-standard function call.

Quote:
>   printf("%s",response);
>  printf("\nClosing socket `sock\'...");
>  shutdown(sock);

Non-standard function call.

Quote:
>  printf("closed\n");
>  return(0);
>  exit(0);

This line will never be executed.

Quote:
> }
> $ cat sockfork.h
> /* --- include headers --- */
> #include <stdio.h>
> #include <sys/socket.h>

Non-standard header.

Quote:
> /* --- macro's --- */
> #define REMOTE_SERV "www.redhat.com"
> #define TCP_PORT    80
> When i try and compile it, e.g. gcc -o sockfork sockfork.c i get an error
> with connect about pointers :-( i haven't quite made my way to pointers...
> If someone could help that would be great.

You're trying to assign the return value of recv() to an array. That
is a constraint violation, as arrays are not modifiable lvalues. Try
the memcpy() function instead.

No offense, but if you haven't made your way to pointers, I suggest
you stick to simple programs and do not tackle advanced features like
sockets. After all, pointers are a pretty important part of the C
language, and C programmers should take time to get them right.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"A friend of mine is into Voodoo Acupuncture. You don't have to go into her
office. You'll just be walking down the street and... ohh, that's much better!"
   - Stephen Wright



Mon, 04 Apr 2005 12:46:49 GMT  
 newbie help with sockets programming...

Quote:

> I wondered if someone can help me... I have this code which
> connects to a socket on the net (http server) and retreived the
> output of a HEAD / to the server. Although ive made a mistake.
> Could someone help me with this ;)

Sockets, and anything not specified by the ISO C standard, are OT
in this group. We deal only with the portable aspects of the
language.  You would be better off in a group dealing with your
particular system, or possibly Unix.

Offhand I can see no use of pointers in your code, apart from the
constant strings.

... snip code ...

Quote:

> When i try and compile it, e.g. gcc -o sockfork sockfork.c i get
> an error with connect about pointers :-( i haven't quite made my
> way to pointers... If someone could help that would be great.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


Mon, 04 Apr 2005 15:44:26 GMT  
 newbie help with sockets programming...


Quote:
> Hi,

> I wondered if someone can help me... I have this code which connects to a
> socket on the net (http server) and retreived the output of a HEAD / to
the
> server. Although ive made a mistake. Could someone help me with this ;)

<code snipped>

Quote:
>  result = connect(sock,REMOTE_SERV,TCP_PORT);

<code snipped>

Quote:
> When i try and compile it, e.g. gcc -o sockfork sockfork.c i get an error
> with connect about pointers :-( i haven't quite made my way to pointers...
> If someone could help that would be great.

Without you posting the exact error message, it's hard to say what is
causing your error.  But I suspect you aren't passing in a correct parameter
(or parameters) to this function.  What I mean is that the type of the
parameter you are trying to pass in does not match the type specified in the
definition of the connect() function.

I would also check your other functions, like socket(), etc. to make sure
you are passing in the correct types.

To be more on topic in the future, I think you should have posted the
prototypes for all the non-standard functions like connect(), socket(), etc.
Then an answer to your question would not rely on knowledge of a
non-standard library of functions.

                    Dan



Tue, 05 Apr 2005 06:28:08 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. : Newbie socket help

2. Socket programming urgent help needed

3. HELP Question SMTP & socket programming

4. socket programming--help please

5. help with socket programming in C (unix)

6. HELP -- Socket programming problem in Linux

7. Help on C socket programming

8. Simple Socket Programming Examples? Help

9. Sockets Programming Help!

10. Need help in DCOM and socket programming between NT and Unix

11. problem with socket-programming (Beginner needs help)

12. Socket Programming (How to program a timeout mechanism on a read/write call)

 

 
Powered by phpBB® Forum Software