forked from mirrors/qotd
57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
#include <arpa/inet.h> // inet_addr()
|
|
#include <netdb.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h> // bzero()
|
|
#include <sys/socket.h>
|
|
#include <unistd.h> // read(), write(), close()
|
|
#include <errno.h>
|
|
#include "config.h"
|
|
#define MAX 80
|
|
#define PORT 17
|
|
#define SA struct sockaddr
|
|
|
|
int main()
|
|
{
|
|
int sockfd, connfd;
|
|
struct sockaddr_in servaddr4, cli;
|
|
|
|
unsigned int servaddrlength;
|
|
// socket create and verification
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (sockfd == -1)
|
|
{
|
|
printf("socket creation failed...\n");
|
|
exit(0);
|
|
}
|
|
else
|
|
printf("Socket successfully created..\n");
|
|
|
|
// assign IP, PORT
|
|
servaddr4.sin_family = AF_INET;
|
|
servaddr4.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
servaddr4.sin_port = htons(PORT);
|
|
|
|
int status = connect(sockfd, (SA *)&servaddr4, sizeof servaddr4);
|
|
// connect the client socket to server socket
|
|
if (status != 0)
|
|
{
|
|
printf("%i\n", errno);
|
|
printf("connection with the server failed...\n");
|
|
|
|
exit(0);
|
|
}
|
|
else
|
|
printf("connected to the server..\n");
|
|
|
|
struct sockaddr *servaddr = (SA *)&servaddr4;
|
|
unsigned int ss = sizeof servaddr;
|
|
// function for chat
|
|
char clientMessage[MESSAGE_STRING_LENGTH] = {0};
|
|
recvfrom(sockfd, clientMessage, MESSAGE_STRING_LENGTH, 0, servaddr, &ss);
|
|
printf(clientMessage);
|
|
|
|
// close the socket
|
|
close(sockfd);
|
|
} |