Multiple Client Server Program in C using fork | Socket Programming

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 109

  • @mwesigyerob1576
    @mwesigyerob1576 6 років тому +13

    this video is good, it's like milky way

    • @IdiotDeveloper
      @IdiotDeveloper  6 років тому +1

      Thanks

    • @Blackoutfor10days
      @Blackoutfor10days 8 місяців тому +2

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

  • @manvelbeaver520
    @manvelbeaver520 5 років тому +11

    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 4 роки тому

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

    • @ishwaryaj9789
      @ishwaryaj9789 4 роки тому

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

  • @PTeja-tr1qw
    @PTeja-tr1qw 3 роки тому +2

    Thank you!! For helping many people through your explanation

  • @asifshahzad4175
    @asifshahzad4175 6 років тому +4

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

  • @X3eRo0
    @X3eRo0 4 роки тому +7

    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 3 роки тому

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

  • @hirenpatel6118
    @hirenpatel6118 4 роки тому +3

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

  • @tuhinsubhrahazra9164
    @tuhinsubhrahazra9164 2 роки тому +1

    Thank you so much . You solve my problem .

  • @V1337adim
    @V1337adim 4 роки тому +2

    Thanks, helped to understand sockets in Linux

  • @anguswylie582
    @anguswylie582 5 років тому +2

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

  • @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.

  • @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?

  • @entropy79
    @entropy79 6 років тому

    This is good one. Keep doing god work !

  • @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

  •  5 років тому +2

    Nice Video, Can you limit the connection to 100 client in same time only? Can you do this example please?

  • @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:"

  • @hussef_sef
    @hussef_sef 2 роки тому

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

  • @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

  • @namangoel4940
    @namangoel4940 3 роки тому

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

  • @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?

  • @nihedzioua9913
    @nihedzioua9913 5 років тому

    thank you so much you were very helpful

  • @aishwaryagunasekaran3106
    @aishwaryagunasekaran3106 4 роки тому +1

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

  • @vasilisa.loupas9014
    @vasilisa.loupas9014 5 років тому

    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.

  • @mrunaldhoble
    @mrunaldhoble 5 років тому +1

    thank you it was really helpfull

  • @abdullahhussein6023
    @abdullahhussein6023 7 років тому

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

  • @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?

  • @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

  • @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?!

  • @itsmia6613
    @itsmia6613 6 років тому

    Thank you, your video was really helpful!

  • @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 3 роки тому

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

    • @KritikaEntertainment
      @KritikaEntertainment 3 роки тому +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){

  • @Blackoutfor10days
    @Blackoutfor10days 8 місяців тому +1

    17:40 main thing

  • @poojabhusare458
    @poojabhusare458 5 років тому +1

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

    • @IdiotDeveloper
      @IdiotDeveloper  5 років тому

      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.

  • @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);

  • @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 ?

  • @jantovornik
    @jantovornik 5 років тому

    Is this type of connection asynchronous?

  • @ishwaryaj9789
    @ishwaryaj9789 4 роки тому +1

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

    • @Xp-Sam
      @Xp-Sam 4 роки тому

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

    • @ishwaryaj9789
      @ishwaryaj9789 4 роки тому

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

    • @Xp-Sam
      @Xp-Sam 4 роки тому

      @@ishwaryaj9789 nope...

    • @ishwaryaj9789
      @ishwaryaj9789 4 роки тому

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

  • @lazamh4869
    @lazamh4869 6 років тому +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.

  • @tabassihasnae411
    @tabassihasnae411 5 років тому +1

    thank you so much

  • @wizamit
    @wizamit 6 років тому

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

    • @IdiotDeveloper
      @IdiotDeveloper  6 років тому

      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 6 років тому

      @@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.

  • @lazyXXrasta
    @lazyXXrasta 7 років тому

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

    • @IdiotDeveloper
      @IdiotDeveloper  7 років тому

      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??

  • @bhawnakamra8005
    @bhawnakamra8005 7 років тому

    thank you.it is really nice

    • @IdiotDeveloper
      @IdiotDeveloper  7 років тому

      +bhawna kamra you welcome. Please share and subscribe

  • @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

  • @PrakashChAwal
    @PrakashChAwal 2 роки тому

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

  • @sachcha_engineer
    @sachcha_engineer 4 роки тому +1

    How to share files through this?

  • @KanwMpam
    @KanwMpam 4 роки тому

    Thank you!

  • @katoutou5382
    @katoutou5382 4 роки тому +1

    What if we had 5 clients from fork?

    • @IdiotDeveloper
      @IdiotDeveloper  4 роки тому

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

  • @DavidCourtney
    @DavidCourtney 5 років тому

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

  • @atreides1890
    @atreides1890 7 років тому

    thank you

  • @abdullahhussein6023
    @abdullahhussein6023 7 років тому

    i love like this program

  • @SandeepSSMishra
    @SandeepSSMishra 4 роки тому

    Please make a video to explain it

  • @sandipjogdand2044
    @sandipjogdand2044 6 років тому

    How to create header file sys/socket.h

  • @abdullahhussein6023
    @abdullahhussein6023 7 років тому

    Please tell me how to do
    multi Cline one server plllllllllllease

    • @IdiotDeveloper
      @IdiotDeveloper  7 років тому +1

      I think this program is a multi-client server.

  • @AdityaKumar-rk3rq
    @AdityaKumar-rk3rq 7 років тому

    thanks for code

    • @IdiotDeveloper
      @IdiotDeveloper  7 років тому

      +Aditya Kumar Your welcome. Do subscribe and share.

  • @abdullahhussein6023
    @abdullahhussein6023 7 років тому

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

    • @IdiotDeveloper
      @IdiotDeveloper  7 років тому

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

  • @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