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