Hey,
I'm new to Socket Programming, and i've only learned C recently so probably what i'm asking will be very obvious, sorry in advance (=
ok Here's my code

/* 
 * File:   Inside_Client.c
 * Author: Ghaith Hachem and Adel Youssef
 *
 * Created on March 17, 2008, 6:12 AM
 */

#define EXIT_SUCCESS 0
#define PORT 1234
#define IP "192.168.0.1"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/*
 * 
 */

//Variable Definitions

//Hostname will be used later with gethostbyname(), i kept an example below
//char hostname[100];
char    *msg;
int	sockfd;
struct sockaddr_in server_addr;
bool Connected;
//Was used in gethostbyname();
//struct hostent *hp;

int main(int argc, char** argv) {
    
/*
 *This is the gethostbyname example
     strcpy(hostname,HOST);
        if (argc>2)
            { strcpy(hostname,argv[2]); }
	if ((hp = gethostbyname(hostname)) == 0) {
		perror("gethostbyname");
                exit(1);
*/
    
    //Define default options   
    memset (&server_addr, '\0', sizeof (server_addr));
    server_addr.sin_family = AF_INET; 
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = inet_addr(IP);
    
    //Create the Socket
    if (sockfd = socket(PF_INET, SOCK_STREAM, 0) < 0) {
        perror("Could not create socket");
        exit(1);
    };
    
    //Connect
    if (connect(sockfd,(struct sockaddr *)  &server_addr, sizeof(server_addr)) < 0)
    {
        perror("Cannot connect to server");
        exit(2);
    };
    
    printf("Connected to host");
    //Authenticating message for now is "Hello"
    msg = "Hello";
    //Send authentication message
    if (send(sockfd, msg, strlen(msg), 0) < 0) 
    {
	perror("Failed to send authentication");
	exit(3);
    };
    
    Connected = true;
    
    while(Connected) 
    {
        if (recv(sockfd,msg,strlen(msg), 0) < 0) 
        {
            perror("Failed to receive messages, shutting down");
            Connected = false;
            
        };
        //Handle received messages here, for now just print out the message
        printf("%s\n", msg);
        
    };
    close(sockfd);

    return (EXIT_SUCCESS);
}

I'm getting
Socket operation on non-socket on the connect() statement, i did a little trace and found out that sockfd was set to 0 at the time, which will explain the error i suppose, but i'm not sure how it got to 0, what have i done wrong?
thank you

change it to this:

server_addr.sin_family = AF_INET; 
	server_addr.sin_addr.s_addr = inet_aton(IP);
	server_addr.sin_port = htons(PORT);

Hey, it's still giving the same result, my code now is

//Define default options   
    memset (&server_addr, '\0', sizeof (server_addr));
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr = inet_aton(IP);
    server_addr.sin_port = htons(PORT);

i see now whats wrong you missed a couple points.

*msg needs to point /somewhere/... either declared as array[ ] or allocated.

you have to send the "sizeof" your message buffer to the "recv" function... not the length of the string (strlen)

i modified this to work with a public server. adjust your code accordingly

#define EXIT_SUCCESS 0
#define PORT 666            //BOFH Excuse Server
#define IP "193.202.115.241"  

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

char     msg[2000];
int      sockfd, connresult, recvresult, sendresult;
struct   sockaddr_in server_addr;
bool     Connected;

int main(int argc, char** argv) 
{
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr = inet_addr(IP);
    server_addr.sin_port = htons(PORT);
        
    //Create the Socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    printf("sockfd = %d\n",sockfd);
    if (sockfd < 0) 
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(1);
    };
        
    //Connect
    errno=0;
    connresult = connect(sockfd,(struct sockaddr *)&server_addr, 
					  sizeof(server_addr));
    printf("connresult = %d\n",connresult);
    if (connresult < 0)
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(2);
    }    
    printf("Connected to host\n");
      
    // receive
    Connected = true;      
    while(Connected) 
    {
        errno = 0; 
        recvresult = recv(sockfd,msg,sizeof(msg), 0);
        //printf("recvresult = %d\n",recvresult);
        if (recvresult < 0)
        {
            printf("errno %d: %s\n",errno,strerror(errno));
            Connected=false;   
           
        }
        //Handle received messages here
        else if (recvresult > 0)
            printf("%s", msg);
        else
            Connected=false;   
    }
    printf("\n");

    close(sockfd);
    return (EXIT_SUCCESS);
}

Thanks, that work, i can now send and receive messages from the server, just a question,
char msg[2000] declared msg as an array of 2000 charachters, but assigning a value will get me an "incompatible type assignment" i did *msg[2000] and *msg = "hello" and i got it running, but i'm not sure that's right..

So that's what it's used for! heh, ok i hate the way C handles strings :p
Thanks for the help! i'm marking this thread as solved, but just one more question, what do i still need to get this code also compile on windows? i read sth about #ifdef win32 #include <winsock.h> else #include what i need, but just so i'm sure, is there a better way? or maybe an implementation of <sys/socket.h> and other libraries for windows?
Thank you

This question has already been answered. Start a new discussion instead.