sock_inet
This commit is contained in:
parent
b3763ff62c
commit
7012521d08
5 changed files with 245 additions and 0 deletions
54
socket_inet/client.c
Normal file
54
socket_inet/client.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue