mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
18c79b203e
* commit '9d51b08a816693281b2890671e9b5fdcbded5b12': Add return value in deque() Remove the white space Typo in variable name Add the return value in create _heap() updating DIRECTORY.md Fix #509 Increased spead of Cocktail Sort Add new sorting algorithm (Cocktail Sort) Changed function name Add new sorting algorithm updating DIRECTORY.md dynamic array data structure Add syntax highlight index now starts from 1 # Conflicts: # client_server/client.c # sorting/Bubble_Sort_2.c
71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
// Write CPP code here
|
|
#include <netdb.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#define MAX 80
|
|
#define PORT 8080
|
|
#define SA struct sockaddr
|
|
void func(int sockfd)
|
|
{
|
|
char buff[MAX];
|
|
int n;
|
|
for (;;)
|
|
{
|
|
bzero(buff, sizeof(buff));
|
|
printf("Enter the string : ");
|
|
n = 0;
|
|
while ((buff[n++] = getchar()) != '\n')
|
|
;
|
|
write(sockfd, buff, 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int sockfd, connfd;
|
|
struct sockaddr_in servaddr, cli;
|
|
|
|
// socket create and varification
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (sockfd == -1)
|
|
{
|
|
printf("socket creation failed...\n");
|
|
exit(0);
|
|
}
|
|
else
|
|
printf("Socket successfully created..\n");
|
|
bzero(&servaddr, sizeof(servaddr));
|
|
|
|
// assign IP, PORT
|
|
servaddr.sin_family = AF_INET;
|
|
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
servaddr.sin_port = htons(PORT);
|
|
|
|
// connect the client socket to server socket
|
|
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
|
|
{
|
|
printf("connection with the server failed...\n");
|
|
exit(0);
|
|
}
|
|
else
|
|
printf("connected to the server..\n");
|
|
|
|
// function for chat
|
|
func(sockfd);
|
|
|
|
// close the socket
|
|
close(sockfd);
|
|
}
|