Search
 
SCRIPT & CODE EXAMPLE
 

C

server client tcp in C

#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()) != '
')
            ;
        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...
");
            break;
        }
    }
}
   
int main()
{
    int sockfd, connfd;
    struct sockaddr_in servaddr, cli;
   
    // socket create and verification
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        printf("socket creation failed...
");
        exit(0);
    }
    else
        printf("Socket successfully created..
");
    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...
");
        exit(0);
    }
    else
        printf("connected to the server..
");
   
    // function for chat
    func(sockfd);
   
    // close the socket
    close(sockfd);
}
Comment

PREVIOUS NEXT
Code Example
C :: c pointers 
C :: how to compareTo in java 
C :: fgets c 
C :: print hello world in c 
C :: unable to locate package dos2unix 
C :: how to malloc for matrix in c 
C :: bubble sort in c 
C :: binary search tree of strings in c 
C :: converting strings to numbers in c 
C :: print float number completely in C language 
C :: variable swap in c 
C :: convert char number to int in c 
C :: Float and Double Input/Output 
C :: powershell search big files 
C :: c check if character is upper case 
C :: create arrya of chars malloc 
C :: threads in c 
C :: c unused parameter 
C :: writing structures in c 
C :: what is console in sublime text 
C :: hostbuilder add environment variables 
C :: recursion function bangla 
C :: https://www.tiktok.com/@kaiwan.99/video/7115521325766069510?is_from_webapp=1&sender_device=pc&web_id=7083069815002449410 
C :: remove language from jupyter notebook 
C :: npm fs zip 
C :: Print Characters 
C :: c stack 
C :: diiferent between * and & in c 
C :: adding three numbers in c 
C :: changing data type in one line c program 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =