help with newbie code

R

Robert Smith

Here is my first attemped with sockets. All i want my programs to do is to
transfere Hello from one program to another. My code doesn't seem to work.
It looks like it just hangs. Any help would be appreciated.

"server"

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

#define MYPORT 3490

int main()
{
int socket_id, newsocket;
int listen_return;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
char *message = "Hello";
int len_of_message;
int bytes_sent;


socket_id =socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(&(my_addr.sin_zero), '\0',8);

bind(socket_id, (struct sockaddr *)&my_addr,sizeof(struct sockaddr));

while(1)
{

if(listen(socket_id, 10)==-1)
{

printf("Connected\n");

newsocket=accept(socket_id, (struct sockaddr*)&their_addr,
sizeof(struct sockaddr_in));

len_of_message=strlen(message);

bytes_sent=send(newsocket,message,len_of_message,0);

printf("Message sent!\n ");
printf("Number if bytes sent is%d",bytes_sent);

}
else
{
// printf("not connected\n");
}
usleep(1);

}

return 0;
}

********************************
client

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

//#define DEST_IP = 127.0.0.1;
//#define DEST_PORT = 3490;


int main()
{
struct sockaddr_in dest_addr;
int sock_fd;
int bytes_read;
char *message;

dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(3490);
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(dest_addr.sin_zero), '\0', 8);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);


connect(sock_fd, (struct sockaddr *) &dest_addr,sizeof(struct
sockaddr));
bytes_read=recv(sock_fd, message, 100, 0);
printf("bytes read is: %d",bytes_read);
return 0;

}
 
J

Jack Klein

Here is my first attemped with sockets. All i want my programs to do is to
transfere Hello from one program to another. My code doesn't seem to work.
It looks like it just hangs. Any help would be appreciated.

"server"

#include <stdio.h>
#include <stdlib.h>

The two headers above are part of standard C and are topical here.
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

The four headers above are not part of standard C, they are
non-standard extensions provided by your compiler/OS combination.
They are completely off-topic here.

You need to take this question to a group that supports your
particular OS combination, perhaps or one of
the groups.
 

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
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top