View Full Version: Sockets In Linux

C++ Learning Community > C++ for Linux > Sockets In Linux


Title: Sockets In Linux
Description: For a game


nontitle - July 7, 2005 03:23 PM (GMT)
hi,
i was just wondering if anyone knew how to use sockets in linux, like what include do i use or do i have to get something?
sorry im just starting to use c/c++ so i dont know much.

C-Man - July 7, 2005 03:54 PM (GMT)
well if i remeber correctly ( been ages since i did that )

<sys/types.h> // types
<sys/socket.h> // sockets
<netdb.h> // dns
<net/inet.h>

IllegalEnzyme - July 8, 2005 11:43 AM (GMT)
don't you need some libs for dns?

C-Man - July 8, 2005 01:17 PM (GMT)
i'm not sur ebut i think they get linked automaticaly but you better check

Consumed - July 9, 2005 12:03 AM (GMT)
Yes they get linked automatically(except for Solaris iirc). Here's the guide I used to learn BSDsock: http://www.ecst.csuchico.edu/~beej/guide/net/html/ . And here's some code of mine that might help you. It will connect to and download a file from a local UNIX web server (I changed the extension because ipb won't let me upload C files for some odd reason).

-Consumed

nontitle - July 12, 2005 01:06 AM (GMT)
...the download link doesn't work...

Consumed - July 12, 2005 08:11 PM (GMT)
Strange, it comes up with the text in a new browser window for me. :huh:

nontitle - July 12, 2005 10:38 PM (GMT)
this is what i get:
QUOTE
ERROR
The requested URL could not be retrieved

While trying to process the request:

GET /forums/CPPlearningcommunity/index.php?act=Attach&type=post&id=2669744 HTTP/1.1
Host: invisionfree.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
http://invisionfree.com/forums/CPPlearning...?showtopic=4491]http://invisionfree.com/forums/CPPlearning...?showtopic=4491
Cookie: CPPlearningcommunityanonlogin=-1; CPPlearningcommunityforum_read=a%3A4%3A%7Bi%3A15%3Bi%3A1121207816%3Bi%3A1%3Bi%3A1121198927%3Bi%3A6%3Bi%3A1121138159%3Bi%3A9%3Bi%3A1121199814%3B%7D; CPPlearningcommunitymember_id=1380; CPPlearningcommunitypass_hash=d973707ffc74307145c24d892bb855c5; CPPlearningcommunitysession_id=b7288cbe8ef71203c552b4f2db6b49a7; CPPlearningcommunitytopicsread=a%3A1%3A%7Bi%3A4491%3Bi%3A1121207820%3B%7D


The following error was encountered:

    * Invalid Response

The HTTP Response message received from the contacted server could not be understood or was otherwise malformed. Please contact the site operator. Your cache administrator may be able to provide you with more details about the exact nature of the problem if needed.

Your cache administrator is webmaster.
Generated Tue, 12 Jul 2005 22:37:11 GMT by (linuxfirewall) (version)

nontitle - July 12, 2005 10:42 PM (GMT)
could you pm me or put in a post what is in the file?

EDIT: I know that its a problem with my firewall and cache... but i cant do anything about it.

Consumed - July 13, 2005 06:58 AM (GMT)
Ok, here it is. The only reason I was reluctant to post it is because I simply can't stand the way ipb handles indention:
CODE

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

int main(int argc, char *argv[])
{
int sock, port = 80, retval;
char str[] = "GET /\r\n", addr[] = "127.0.0.1";
char buffer[65536];
struct sockaddr_in sa;
FILE *f;

sa.sin_family  = AF_INET;
sa.sin_port   = htons(port);
sa.sin_addr.s_addr = inet_addr(addr);
memset(&(sa.sin_zero), '\0', 8);

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock < 1)
{
 sprintf(buffer, "socket() on line %d in file %s", __LINE__, __FILE__);
 perror(buffer);
 close(sock);

 return 1;
}

printf("Created socket.\n");

retval = connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr));
if(retval < 1)
{
 sprintf(buffer, "connect() on line %d in file %s", __LINE__, __FILE__);
 perror(buffer);

 if(errno != EINPROGRESS)
 {
  close(sock);
  return 1;
 }
}

printf("Connection established to the remote host.\n");
sleep(2);

retval = send(sock, str, strlen(str), 0);
if(retval < 1)
{
 sprintf(buffer, "send() on line %d in file %s", __LINE__, __FILE__);
 perror(buffer);
 close(sock);

 return 1;
}

printf("Sent %d bytes of data to host.\n", retval);

retval = recv(sock, buffer, 65536, 0);
if(retval < 1)
{
 sprintf(buffer, "recv() on line %d in file %s", __LINE__, __FILE__);
 perror(buffer);
 close(sock);

 return 1;
}

printf("Recieved %d bytes of data from host:\n%s", retval, buffer);
printf("Saving html to file 'socktut.html'.\n");

f = fopen("socktut.html", "w+");
if(f == NULL)
{
 sprintf(buffer, "fopen() on line %d in file %s", __LINE__, __FILE__);
 perror(buffer);
 close(sock);
 return 1;
}

fputs(buffer, f);
fclose(f);

printf("\nShutting down.\n");
close(sock);

return 0;
}

nontitle - July 14, 2005 12:29 AM (GMT)
well at first it didn't work cause you didnt #include <string>, but i fixed that, but the ip was set to 127.0.0.1 (was that purposeful?) so i switched it to googles ip and it worked for a but but it hanged at the sent %d(7 in my case) bytes of data, which im guessing is because it didn't receive the data back? Can someone help?




Hosted for free by InvisionFree