54 lines
1.2 KiB
C
54 lines
1.2 KiB
C
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#define MAX 80
|
|
#define PORT 8080
|
|
|
|
void func(int sockfd) {
|
|
char buff[MAX];
|
|
|
|
for (;;) {
|
|
char *msg = "Ping\n";
|
|
write(sockfd, msg, sizeof(buff));
|
|
|
|
bzero(buff, sizeof(buff));
|
|
read(sockfd, buff, sizeof(buff));
|
|
|
|
printf("From Server : %s", buff);
|
|
if ((strncmp(buff, "exit", 4)) == 0) {
|
|
printf("Client Exit...\n");
|
|
break;
|
|
}
|
|
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (sockfd == -1) {
|
|
printf("socket creation failed...\n");
|
|
exit(0);
|
|
}
|
|
printf("Socket successfully created..\n");
|
|
|
|
struct sockaddr_in servaddr;
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
servaddr.sin_family = AF_INET;
|
|
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
servaddr.sin_port = htons(PORT);
|
|
|
|
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
|
|
printf("connection with the server failed...\n");
|
|
exit(0);
|
|
}
|
|
|
|
func(sockfd);
|
|
close(sockfd);
|
|
}
|