Multiple Client Server Program in C using fork | Socket Programming

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ต.ค. 2024
  • In this video, we are going to build a client-server program in the C programming language to communicate over the network. Here the server is going to be a concurrent server, which has the ability to handle multiple clients at the same time using the fork system call. The client is going to a simple program that sends a message to the server and the server send the same message back to the client.
    FORK SYSTEM CALL: www.csl.mtu.edu...
    An echo server is a server that sends back the same message that the client has sent the server.
    Like:
    Client send - abc
    Server recv - abc
    SOURCE CODE: github.com/nik...
    FOLLOW ME:
    FACEBOOK: / idiotdeveloper
    TWITTER: / nikhilroxtomar
    INSTAGRAM: instagram/nikhilroxtomar
    PATREON: / idiotdeveloper
    WEBSITE: idiotdeveloper...
    MY GEARS:
    Intel i5-7400: amzn.to/3ilpq95
    Gigabyte GA-B250M-D2V: amzn.to/3oPuntd
    ZOTAC GeForce GTX 1060: amzn.to/2XNtsxn
    LG 22MP68VQ 22 inch IPS Monitor: amzn.to/3soUKs5
    Corsair VENGEANCE LPX 16GB: amzn.to/2LVyR2L
    WD Green 240 GB SSD: amzn.to/3igt1Ft
    1TB WD Blue: amzn.to/38I6uhw
    Corsair VS550 550W: amzn.to/3nILHi3
    Zebronics BT4440RUCF 4.1 Speakers: amzn.to/2XGu203
    Segate 1TB Portable Hard Disk: amzn.to/3bF8YPG
    Seagate Backup Plus Hub 8 TB External HDD: amzn.to/39wcqtj
    Maono AU-A04 Condenser Microphone: amzn.to/35HHiWC
    Techlicious 3.5mm Clip Microphone: amzn.to/3bERKSD
    Redgear Dagger Headphones: amzn.to/3ssZNYr

ความคิดเห็น • 108

  • @mwesigyerob1576
    @mwesigyerob1576 6 ปีที่แล้ว +12

    this video is good, it's like milky way

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว +1

      Thanks

    • @Blackoutfor10days
      @Blackoutfor10days 4 หลายเดือนก่อน +2

      ​@@IdiotDevelopertumhara bohot bohot shukriya tumhare wajah se me exam pass hua🥺

  • @manvelbeaver520
    @manvelbeaver520 4 ปีที่แล้ว +10

    I love you. You saved me on a big assignment for handling multiple clients with sockets for my last class to graduate. Thank you.

    • @Xp-Sam
      @Xp-Sam 3 ปีที่แล้ว

      I am stucked in the same situation... Maybe u can help

    • @ishwaryaj9789
      @ishwaryaj9789 3 ปีที่แล้ว

      I tried the same program I am not able to do can u please help me

  • @PTeja-tr1qw
    @PTeja-tr1qw 2 ปีที่แล้ว +2

    Thank you!! For helping many people through your explanation

  • @X3eRo0
    @X3eRo0 4 ปีที่แล้ว +6

    there is a buffer overflow when using scanf("%s", buffer); because scanf("%s") doesnt check how many bytes are entered. may result in a DoS or remote code execution.

    • @IdiotDeveloper
      @IdiotDeveloper  4 ปีที่แล้ว +1

      Thanks for the information.

    • @X3eRo0
      @X3eRo0 4 ปีที่แล้ว +5

      @@IdiotDeveloper the fix is scanf with "1024s" as format specifier to tell scanf that only read 1024 bytes no more than that

    • @coverscollection8775
      @coverscollection8775 2 ปีที่แล้ว

      @@X3eRo0 hi can you help me fixing the garbage value in the server output? I tried your method but its still

  • @asifshahzad4175
    @asifshahzad4175 6 ปีที่แล้ว +4

    Hi sir , the way you speak is so funny by the way informative video thanks

  • @hirenpatel6118
    @hirenpatel6118 3 ปีที่แล้ว +3

    Sorry, why did you close the sockfd socket after fork call?

  • @tuhinsubhrahazra9164
    @tuhinsubhrahazra9164 2 ปีที่แล้ว +1

    Thank you so much . You solve my problem .

  • @anguswylie582
    @anguswylie582 5 ปีที่แล้ว +2

    Great video you're very clear and concise, helped a lot!!

    • @IdiotDeveloper
      @IdiotDeveloper  5 ปีที่แล้ว

      Thanks bro. Do share and subscribe

  • @V1337adim
    @V1337adim 4 ปีที่แล้ว +2

    Thanks, helped to understand sockets in Linux

  • @gamezone4470
    @gamezone4470 4 ปีที่แล้ว

    you are a fantastic, thank you very much ..... I have a question, I don't know if you can help me but I want to know how to create multiple processes that connect to the apache web server, establish a connection and keep it waiting, just wait for the server timeout , and when the connection is lost, the process should restart it.
    In the end what I want is to see the simultaneous connections the web server can support, using a C language and fork () processes, the parent process will count how many processes achieve controls and many don't.

  • @christopher6752
    @christopher6752 4 ปีที่แล้ว +1

    Super cool thanks could you make an explanatory video on how to implement a multiplayer mode with sockets in a game c ?

    • @IdiotDeveloper
      @IdiotDeveloper  4 ปีที่แล้ว

      Can you explain it with little more details.

    • @christopher6752
      @christopher6752 4 ปีที่แล้ว +1

      @@IdiotDeveloper I was thinking of a basic shooting game from scratch or with a library in which we could code a LAN with sockets or something like it

  • @mehdicharife2335
    @mehdicharife2335 2 ปีที่แล้ว +1

    I don't understand why the server's socket is closed while it is still needed. Can someone explain why is that?

  • @vasilisa.loupas9014
    @vasilisa.loupas9014 4 ปีที่แล้ว

    Very interesting video.
    Can you please tell me how to make the server wait for a user message and then send it to the client (chat), and not responding with the same message.
    Thank you.

  • @entropy79
    @entropy79 6 ปีที่แล้ว

    This is good one. Keep doing god work !

  • @nihedzioua9913
    @nihedzioua9913 4 ปีที่แล้ว

    thank you so much you were very helpful

  • @mrunaldhoble
    @mrunaldhoble 5 ปีที่แล้ว +1

    thank you it was really helpfull

  • @switchups794
    @switchups794 3 ปีที่แล้ว

    Hello, how would I approach making the server ask the client for a name so that "printf" would print the client's name that was received instead of printing "client:"

  • @Gabriel-iy1zq
    @Gabriel-iy1zq 4 ปีที่แล้ว

    what is the difference of sockfd and newSocket and why do I need to initialize the struct sock_addr twice?

  • @husseinyusuf8945
    @husseinyusuf8945 ปีที่แล้ว

    is it possible for the cilient to chat with each other? if yes, how?

  • @abdullahhussein6023
    @abdullahhussein6023 6 ปีที่แล้ว

    Thank you very much
    This lesson is very important
    God bless you
    We will not deny you.

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Welcome. Do Share and Subscribe.

  • @namangoel4940
    @namangoel4940 3 ปีที่แล้ว

    Can we also read the data from first client to another client in socket ?

  • @itsmia6613
    @itsmia6613 6 ปีที่แล้ว

    Thank you, your video was really helpful!

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Your Welcome. Do share and subscribe.

  • @Blackoutfor10days
    @Blackoutfor10days 4 หลายเดือนก่อน +1

    17:40 main thing

  • @ramub4209
    @ramub4209 4 ปีที่แล้ว

    Can you please elaborate more on what does code do at line 56, fork creates a new process and we are closing the child but receiving the data inside child only, how is this working? there is no code to execute for parent process

  • @aishwaryagunasekaran3106
    @aishwaryagunasekaran3106 3 ปีที่แล้ว +1

    why there is close(sockfd) in the server program after fork??

  • @ohmsimon9753
    @ohmsimon9753 6 ปีที่แล้ว

    Thanks , very helpful.
    But how we can limit the number of clients that are connected to the server, for example 10 clients ?????

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      just specify the number of clients you wants in the listen commands.
      listen(sockfd,10);

  • @javaclass
    @javaclass 6 ปีที่แล้ว +6

    We can avoid garbage value which display in server by using this code
    int rv=recv(newsockfd,buffer,1024,0);
    buffer[rv]='\0';
    printf("%s",buffer);
    then it will not display garbage value. because when it is transferred in transfer one by one character. like as character array. then we make it string by using NULL character. '\0' . Any Networking program visit google and search javatech 123

    • @coverscollection8775
      @coverscollection8775 2 ปีที่แล้ว

      hi can you help me fixing the garbage value in the server output?

    • @KritikaEntertainment
      @KritikaEntertainment 2 ปีที่แล้ว +1

      @@coverscollection8775 @Covers Collection you just add two lines in while loop in server program.
      while(1){
      int size=recv(newSocket, buffer, 1024, 0);
      buffer[size]='\0';
      if(strcmp(buffer, "exit") == 0){

  • @pruthashinde2934
    @pruthashinde2934 4 ปีที่แล้ว

    You said we need to make some changes to make it chat application so can I get to know what are those?!

  • @poojabhusare458
    @poojabhusare458 4 ปีที่แล้ว +1

    How to connect client from different system.
    Where I have to change in client and server program

    • @IdiotDeveloper
      @IdiotDeveloper  4 ปีที่แล้ว

      The two computers must be connected to each other like in a LAN. Then use the server computer ip address instead of localhost. Then on client computer use the server ip address for connecting to server.

  • @selvapk5567
    @selvapk5567 3 ปีที่แล้ว

    Concurrent Simultaneous Action
    Hi i am having 2 laptops with different user names but same OS & settings, files.
    If i open control panel and change some settings say mouse sensitivity in laptop 1, the same should happen live/real time in laptop 2 and vice versa.
    Is is possible?

  • @jantovornik
    @jantovornik 5 ปีที่แล้ว

    Is this type of connection asynchronous?

  • @ashisranjandey7541
    @ashisranjandey7541 6 ปีที่แล้ว +1

    Why are u closing the socket in child process --line 57 in tcpServer...plz explain

    • @brianfiszman3179
      @brianfiszman3179 6 ปีที่แล้ว +11

      When you fork a process you clone the process and store its id in pid. How does the program knows which process is it working on?
      Simple! If pid == 0 it means you are in the child process. If you are in the parent process, then the pid variable would have another value, and thats because the fork() returns twice.
      So... going back to your original question... why close the socket in child process? Well you might have realized that there are two sockets... or socket decriptors. There is sockfd (the socket that the server uses to listen for new clients) and newSocket (the socket the server send() and recv() directly to and from the connected client).
      So... the forked process is used for writing and reading from the newSocket, not for listening, that work belongs to the parent process, so whatever you execute inside if(pid == 0){} block would only happen inside the child process, not in the parent. The sockfd would be closed in the child process and not in the parent... so the parent would continue to listen for connections and the child would use the newSocket returned by the accept() to send and recv messages.
      Why forking the process? Well, multitasking of course! Threads are complicated and forking tends to be a much better idea. You might have realized he is sending and receiving messages at the same time, thats thanks to fork().
      Sorry I made it too long. I hope I was of help!

    • @celestinejoy9519
      @celestinejoy9519 2 ปีที่แล้ว

      @@brianfiszman3179 thanks it was really helpful

  • @lazamh4869
    @lazamh4869 5 ปีที่แล้ว +1

    Excuse me How do I connect 2 different machines?
    Please help me

    • @jozefgoral6812
      @jozefgoral6812 3 ปีที่แล้ว

      I had the same problem. You can do it, by setting in client code server's IP address. Moreover you have to set one line like this: serverAddr.sin_addr.s_addr = INADDR_ANY. Then you can connect many clients from different machines, which working LAN. If you want connect with Virtualbox machine, you should set network card as bridged.

  • @PrakashChAwal
    @PrakashChAwal 2 ปีที่แล้ว

    How to implement concurrent server without using fork system call. Please explain in briefly.

  • @manasananda6728
    @manasananda6728 4 ปีที่แล้ว

    Can we use server class in c language and client class in java language and establish connection between them ?

    • @IdiotDeveloper
      @IdiotDeveloper  4 ปีที่แล้ว +1

      I think both the program should work perfectly fine.

    • @manasananda6728
      @manasananda6728 4 ปีที่แล้ว

      @@IdiotDeveloper can u please provide same program ?

  • @sachcha_engineer
    @sachcha_engineer 3 ปีที่แล้ว +1

    How to share files through this?

  • @tabassihasnae411
    @tabassihasnae411 5 ปีที่แล้ว +1

    thank you so much

  • @wizamit
    @wizamit 5 ปีที่แล้ว

    What does "childpid = fork()" means?
    What is it for?
    How to limit the number of client?

    • @IdiotDeveloper
      @IdiotDeveloper  5 ปีที่แล้ว

      System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.
      You can specify the number of clients in listen method.
      If you wants to limit upto N clients, then specify N in listen method.listen(sockfd, N)

    • @wizamit
      @wizamit 5 ปีที่แล้ว

      @@IdiotDeveloper But that indicates how many client can wait. I'm currently using a counter and accept only if less than 3 client is connected. But sometime it doesn't work. As it's not always able to count the disconnected clients.

  • @artahir123
    @artahir123 ปีที่แล้ว

    Sorry Sir
    but what thing you explained ?
    you were just speaking what you were writing......
    anyone can correct me if I'm wrong

    • @IdiotDeveloper
      @IdiotDeveloper  ปีที่แล้ว

      I agree with you. This tutorial was recorded when I was new to content creation. I just somehow want to implement the program and don't think from the perspective for new person.

    • @artahir123
      @artahir123 ปีที่แล้ว

      @@IdiotDeveloper ok sir

  • @bhawnakamra8005
    @bhawnakamra8005 6 ปีที่แล้ว

    thank you.it is really nice

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      +bhawna kamra you welcome. Please share and subscribe

  • @DavidCourtney
    @DavidCourtney 5 ปีที่แล้ว

    How do you shutdown/exit the server without CTRL+C?

  • @katoutou5382
    @katoutou5382 3 ปีที่แล้ว +1

    What if we had 5 clients from fork?

    • @IdiotDeveloper
      @IdiotDeveloper  3 ปีที่แล้ว

      I am not sure about that, but I think the server should work fine for 5 clients.

  • @lazyXXrasta
    @lazyXXrasta 6 ปีที่แล้ว

    how would you make this program to send a message from Client A, go though server and end up in client B?

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Then we need to make the server a router, to route message from client A to B and vice-versa.

    • @KanwMpam
      @KanwMpam 4 ปีที่แล้ว

      did you find the answer??

  • @ishwaryaj9789
    @ishwaryaj9789 3 ปีที่แล้ว +1

    Hi how can I reach out to u I need help in this

    • @Xp-Sam
      @Xp-Sam 3 ปีที่แล้ว

      same.... I am doing a project and need his help

    • @ishwaryaj9789
      @ishwaryaj9789 3 ปีที่แล้ว

      @@Xp-Sam did u figured it out how to do

    • @Xp-Sam
      @Xp-Sam 3 ปีที่แล้ว

      @@ishwaryaj9789 nope...

    • @ishwaryaj9789
      @ishwaryaj9789 3 ปีที่แล้ว

      @@Xp-Sam did u follow the way he did and u got something?

  • @sandipjogdand2044
    @sandipjogdand2044 6 ปีที่แล้ว

    How to create header file sys/socket.h

  • @KanwMpam
    @KanwMpam 4 ปีที่แล้ว

    Thank you!

    • @IdiotDeveloper
      @IdiotDeveloper  4 ปีที่แล้ว

      Make sure to share and subscribe

  • @SandeepSSMishra
    @SandeepSSMishra 4 ปีที่แล้ว

    Please make a video to explain it

  • @abdullahhussein6023
    @abdullahhussein6023 6 ปีที่แล้ว

    i love like this program

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Thanks.

    • @abdullahhussein6023
      @abdullahhussein6023 6 ปีที่แล้ว

      Give me
      the file please

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Download it from GITHUB
      github.com/nikhilroxtomar/Multiple-Client-Server-Program-in-C-using-fork

  • @abdullahhussein6023
    @abdullahhussein6023 6 ปีที่แล้ว

    Please tell me how to do
    multi Cline one server plllllllllllease

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว +1

      I think this program is a multi-client server.

  • @atreides1890
    @atreides1890 6 ปีที่แล้ว

    thank you

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Welcome. Do Share and Subscribe.

  • @abdullahhussein6023
    @abdullahhussein6023 6 ปีที่แล้ว

    please Give me
    the file Cline and server.c or cc

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      Download it from GITHUB
      github.com/nikhilroxtomar/Multiple-Client-Server-Program-in-C-using-fork

  • @AdityaKumar-rk3rq
    @AdityaKumar-rk3rq 6 ปีที่แล้ว

    thanks for code

    • @IdiotDeveloper
      @IdiotDeveloper  6 ปีที่แล้ว

      +Aditya Kumar Your welcome. Do subscribe and share.

  • @luispinto2744
    @luispinto2744 2 ปีที่แล้ว

    #burlão

  • @shockingvideos5225
    @shockingvideos5225 6 ปีที่แล้ว +1

    Why are u closing the socket in child process --line 57 in tcpServer...plz explain

    • @brianfiszman3179
      @brianfiszman3179 6 ปีที่แล้ว +6

      When you fork a process you clone the process and store its id in pid. How does the program knows which process is it working on?
      Simple! If pid == 0 it means you are in the child process. If you are in the parent process, then the pid variable would have another value, and thats because the fork() returns twice.
      So... going back to your original question... why close the socket in child process? Well you might have realized that there are two sockets... or socket decriptors. There is sockfd (the socket that the server uses to listen for new clients) and newSocket (the socket the server send() and recv() directly to and from the connected client).
      So... the forked process is used for writing and reading from the newSocket, not for listening, that work belongs to the parent process, so whatever you execute inside if(pid == 0){} block would only happen inside the child process, not in the parent. The sockfd would be closed in the child process and not in the parent... so the parent would continue to listen for connections and the child would use the newSocket returned by the accept() to send and recv messages.
      Why forking the process? Well, multitasking of course! Threads are complicated and forking tends to be a much better idea. You might have realized he is sending and receiving messages at the same time, thats thanks to fork().
      Sorry I made it too long. I hope I was of help!

    • @arpitvarshney9905
      @arpitvarshney9905 5 ปีที่แล้ว

      @@brianfiszman3179 really very good thanks man

    • @mehdicharife2335
      @mehdicharife2335 2 ปีที่แล้ว

      @@brianfiszman3179 I don't understand. How can the client's socket inside the if(pid==0){} block be the same as the one that was accepted by the parent while the server's socket is different?

    • @waynefalzon1781
      @waynefalzon1781 2 ปีที่แล้ว

      @@brianfiszman3179 Thanks very helpful and clear