chat c socket tcp geeksforgeeks
C
//Client-side TCP connection chat
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.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);
}
// Server-side TCP connection chat
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// 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");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
}
Also in C:
- Title
- how to put a char into a string c
- Category
- C
- Title
- c bitwise operators
- Category
- C
- Title
- functions return type in c
- Category
- C
- Title
- how to free a 2d array correctly
- Category
- C
- Title
- c value set to zero __memmove_avx_unaligned_erms
- Category
- C
- Title
- es palindromo
- Category
- C
- Title
- prime number
- Category
- C
- Title
- coin row problem in linear time
- Category
- C
- Title
- multiplicacion recursiva
- Category
- C
- Title
- function pointer c
- Category
- C
- Title
- c program to find the sum of given number using recursion
- Category
- C
- Title
- delete string function in c
- Category
- C
- Title
- sbatch array set max jobs at once
- Category
- C
- Title
- objective c swizzle method
- Category
- C
- Title
- c concatenate strings
- Category
- C
- Title
- how to convert int in to const char in c
- Category
- C
- Title
- california
- Category
- C
- Title
- read from stdin c
- Category
- C
- Title
- #include<stdlib.h>
- Category
- C
- Title
- c copy string
- Category
- C
- Title
- link whatsapp to website
- Category
- C
- Title
- too few arguments to function in c
- Category
- C
- Title
- disable gnu++11 option
- Category
- C
- Title
- how to print in c
- Category
- C
- Title
- Couldn't create temporary file to work with
- Category
- C
- Title
- what are the causes of memory leaks in c
- Category
- C
- Title
- keras conv2d batchnorm
- Category
- C
- Title
- passing 2d array as parameter to function in c
- Category
- C
- Title
- c pause for 1 second
- Category
- C
- Title
- switch case c
- Category
- C
- Title
- debian install npm
- Category
- C
- Title
- read a binary file c
- Category
- C
- Title
- find string in all files powershell
- Category
- C
- Title
- how to only show tenths place in c
- Category
- C
- Title
- fibonacci series using recursion
- Category
- C
- Title
- how to store a user input with spaces in c
- Category
- C
- Title
- convert string to float c
- Category
- C
- Title
- how to login to another user in powershell
- Category
- C
- Title
- c vs python
- Category
- C
- Title
- how to comment in arduino
- Category
- C
- Title
- restapi
- Category
- C
- Title
- c check if file was created
- Category
- C
- Title
- Switch Mode C Programming
- Category
- C
- Title
- string to int c
- Category
- C
- Title
- c print hello world
- Category
- C
- Title
- undefined symbols for architecture x86_64 in c
- Category
- C
- Title
- c how to define a variable
- Category
- C
- Title
- vbl share price
- Category
- C
- Title
- how to write function in c
- Category
- C
- Title
- add border to image android
- Category
- C
- Title
- duplicar cadena
- Category
- C
- Title
- how to get user input in c
- Category
- C
- Title
- print in c
- Category
- C
- Title
- battlefield4u.com
- Category
- C
- Title
- '&&' within '||'
- Category
- C
- Title
- /usr/bin/mandb: fopen /var/cache/man/7935: Permission denied
- Category
- C
- Title
- c radians
- Category
- C
- Title
- uninstall elgg from hostgtor
- Category
- C
- Title
- how to remove button decoration
- Category
- C
- Title
- sum of arrays
- Category
- C
- Title
- doubly linked list c
- Category
- C
- Title
- pass the pointer in C
- Category
- C
- Title
- get_session` is not available when using TensorFlow 2.0.
- Category
- C
- Title
- how to pass an array to a thread in c?
- Category
- C
- Title
- remove element queue
- Category
- C
- Title
- fa fa-facebook
- Category
- C
- Title
- arduino vscode upload choosing sketch
- Category
- C
- Title
- C largest unsigned int
- Category
- C
- Title
- how to feed a char array to function in C
- Category
- C
- Title
- Fibonacci program c pthread
- Category
- C
- Title
- version of libgcc
- Category
- C
- Title
- c printf uint32_t
- Category
- C
- Title
- div
- Category
- C
- Title
- change plot line color in matplotlib
- Category
- C
- Title
- strtok
- Category
- C
- Title
- how to print the address of a pointer in c
- Category
- C
- Title
- wait function in c
- Category
- C
- Title
- bella ciao lyrics
- Category
- C
- Title
- How to pull images from Docker Registry
- Category
- C
- Title
- how to get random numbers in c
- Category
- C
- Title
- linux directory commands
- Category
- C
- Title
- set timezone in debian terminal
- Category
- C
- Title
- c program to find the sum of given number
- Category
- C
- Title
- text wrap terminal colour
- Category
- C
- Title
- diagonales
- Category
- C
- Title
- function in c
- Category
- C
- Title
- last element from list javascript
- Category
- C
- Title
- mediawiki upload size
- Category
- C
- Title
- arduino knn
- Category
- C
- Title
- printf n characters c
- Category
- C
- Title
- print integer to stdout using write or putchar?
- Category
- C
- Title
- char to int c
- Category
- C
- Title
- program using if statement in c whether numnber is less eqaul to greater than 50
- Category
- C
- Title
- sleep in c programming
- Category
- C
- Title
- how to declare 2 d array using malloc
- Category
- C
- Title
- lazer codechef
- Category
- C
- Title
- pi in c language
- Category
- C
- Title
- strtoul C
- Category
- C
- Title
- array length c
- Category
- C
- Title
- what is restrict keyword in c
- Category
- C