answersLogoWhite

0

How write a client server programs using c?

Updated: 8/16/2019
User Avatar

Wiki User

12y ago

Best Answer

I've gotten these working with each other.

CLIENT

Code:

#ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include /// CLIENT int main(int argc, char *argv[]) { printf("This is the client program\n"); int sockfd; int len, RC ; struct sockaddr_in address; int result; char ch = 'A'; //Create socket for client. sockfd = socket(PF_INET, SOCK_STREAM, 0); if (sockfd 'A') sleep(5) ; // pause 5 seconds } close(sockfd); exit(0); }

SERVER

Code:

#ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include /// SERVER int main(int argc, char *argv[]) { //Declaring process variables. int server_sockfd, client_sockfd; int server_len ; int RC ; unsigned client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; //Remove any old socket and create an unnamed socket for the server. server_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htons(INADDR_ANY); server_address.sin_port = htons(7734) ; server_len = sizeof(server_address); RC = bind(server_sockfd, (struct sockaddr *) &server_address, server_len); printf("RC from bind = %d\n", RC ) ; //Create a connection queue and wait for clients RC = listen(server_sockfd, 5); printf("RC from listen = %d\n", RC ) ; client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len); printf("after accept()... client_sockfd = %d\n", client_sockfd) ; while(1) { char ch; printf("server waiting\n"); //Accept a connection //client_len = sizeof(client_address); //client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len); //printf("after accept()... client_sockfd = %d\n", client_sockfd) ; //Read write to client on client_sockfd RC = read(client_sockfd, &ch, 1); printf("RC from read = %d\n", RC ) ; if (ch=='X') break ; ch++; write(client_sockfd, &ch, 1); } printf("server exiting\n"); //close(client_sockfd); close(client_sockfd); return 0; }

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How write a client server programs using c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP


Write and run a client and a server program in C-language using TCP The client?

Homework question, this is a long answer.


How connectionless communication between a client and a server proceeds when using sockets?

Both the client and the server create a socket, but only the server binds the socket to a local endpoint. The server can then subsequently do a blocking readcall in which it waits for incoming data from any client. Likewise, after creating the socket, the client simply does a blocking call to write data to the server. There is no need to close a connection.


Can you write multiple scripting languages in a single page?

Yes. In fact this is fairly common. For example, using in the same web page a client scripting language such as JavaScript and a Server Scripting Language, such as ASP.NET or PHP.


What is server base language?

Server-base language mean code you write will be processed on server instead of client, ex: when you request a page from server (a php page) it will be first processed on server then server will send you result of codes.. it means you won't see php codes on the page only result of it.


Why can only ops write on signs on my minecraft server?

No, everyone should be able to write on them however the server you're playing on may have a plugin preventing you from using signs


What are the differences between client-side programming and server-side programming?

All of us (or most of us) would have started programming in Java with the ever famous "Hello World!" program. If you can recollect, we saved this file with a .java extension and later compiled the program using javac and then executed the class file with java. Apart from introducing you to the language basics, the point to be noted about this program is that - "It is a client side program". This means that you write, compile and also execute the program on a client machine (e.g. Your PC). No doubt, this is the easiest and fastest way to write, compile and execute programs. But, it has little practical significance when it comes to real world programming.


What you 'll do using programming language?

Write computer-programs, I suppose.


What is Http and how does Http work?

ofcourse we have server and clients first step is open the browser and write the domain name as www.google.com then we note the http is write that is meen the protocol http is commnected the server with client by use the ip address that are resolved by DNS


What if you are not an existing user in silab client?

no worries pals! go to this website: www.moparscape.org create and account there by using register now... NOW DONT LOGIN THAT WEBSITE!!! REMEMBER THIS!!! go to silab client in username type the one u chose and in pass enter ur pass in server write this: 67.82.9.75 port will be the same =) to start do this ::starter.... ENJOY!!


How do you write a sentence using the word refute?

The lawyer had no argument to refute the evidence against his client. Refute means to disprove.


How can you access various sites on the WWW by using hyperlinks or?

You can achieve this by in .htaccess code in apache server, if you are using windows server you need to write 3 line script