socket programming instance code in the C language
- 2020-09-28 09:02:06
- OfStack
I have just finished reading c primer plus and gained a lot. I also have a more comprehensive understanding of C language and have more ideas about modularity and data structure. I have learned C language before, but I can't remember much of it and my knowledge is scattered, which is also the reason why I read this book.
In the future, I will study socket communication while learning programming. My goal is to thoroughly study socket and design my own framework. In the future, It will be of great benefit to engage in server development and architecture.
Well, without further ado, I'm looking for the source code on the Internet.
/* window socket Server-side programming tests */
#include <stdio.h> // Used for printf And so on
#include <winsock2.h> //Socket Function call of
#pragma comment (lib, "ws2_32.lib") //C Languages refer to other class libraries except .h In addition to the file, also add the corresponding lib File, if the error is still prompted, it needs to be in IDE Add the link library manually in
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockaddr;
sockaddr.sin_family=PF_INET;
sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); // Which one you need to bind to locally IP address
sockaddr.sin_port=htons(9000); // Ports to listen on
bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR)); // Binding action
listen(s, 1); // Start listening
printf("listening on port [%d].\n", 9000);
while(TRUE)
{
SOCKADDR clientAddr;
int size=sizeof(SOCKADDR);
SOCKET clientsocket;
clientsocket=accept(s, &clientAddr, &size); // Block until there is a new one tcp Client connection
printf("***SYS*** New client touched.\n");
char* msg="Hello, my client.\r\n";
send(clientsocket, msg, strlen(msg)+sizeof(char), NULL); // Here is the first 3 Note that the number of parameters is added 1 a char The length of the
printf("***SYS*** HELLO.\n");
while(TRUE)
{
char buffer[MAXBYTE]={0};
recv(clientsocket, buffer, MAXBYTE, NULL); //1 Receive the client directly socket the send operation
printf("***Client*** %s\n", buffer);
}
closesocket(clientsocket); // Shut down socket
}
closesocket(s); // Close monitoring socket
WSACleanup(); // uninstall
getchar();
exit(0);
}
/* window socket Client-side programming tests */
#include <stdio.h> // Calls to input and output functions ,printf, gets
#include <winsock2.h> //socket The header file
#include <Windows.h> // To facilitate debugging, wait is added 2 Seconds to connect server Here it is sleep function
#pragma comment (lib, "ws2_32") //socket The library files
typedef struct sockaddr_in sockaddr_in;
int main()
{
Sleep(2000); // The sleeping 2 Seconds to connect server
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockaddr;
sockaddr.sin_family=PF_INET;
sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockaddr.sin_port=htons(9000);
connect(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));
char buffer[MAXBYTE]={0};
recv(s, buffer, MAXBYTE, NULL);
printf("***SERVER***%s", buffer);
while(TRUE)
{
char* mymsg = (char *)malloc(sizeof(char) * 100000);
printf("You can chat with server now:\n");
gets(mymsg);
send(s, mymsg, strlen(mymsg)+sizeof(char), NULL);
/*
recv In the function bufferlength Parameters can be fixed
send In the function bufferlength The parameter cannot be fixed, it needs to look at the actual length and take into account '\0' string
*/
}
closesocket(s);
WSACleanup();
getchar();
exit(0);
}
/* window socket Client-side programming tests */
#include <stdio.h> // Calls to input and output functions ,printf, gets
#include <winsock2.h> //socket The header file
#include <Windows.h> // To facilitate debugging, wait is added 2 Seconds to connect server Here it is sleep function
#pragma comment (lib, "ws2_32") //socket The library files
typedef struct sockaddr_in sockaddr_in;
int main()
{
Sleep(2000); // The sleeping 2 Seconds to connect server
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockaddr;
sockaddr.sin_family=PF_INET;
sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockaddr.sin_port=htons(9000);
connect(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));
char buffer[MAXBYTE]={0};
recv(s, buffer, MAXBYTE, NULL);
printf("***SERVER***%s", buffer);
while(TRUE)
{
char* mymsg = (char *)malloc(sizeof(char) * 100000);
printf("You can chat with server now:\n");
gets(mymsg);
send(s, mymsg, strlen(mymsg)+sizeof(char), NULL);
/*
recv In the function bufferlength Parameters can be fixed
send In the function bufferlength The parameter cannot be fixed, it needs to look at the actual length and take into account '\0' string
*/
}
closesocket(s);
WSACleanup();
getchar();
exit(0);
}
1 Failed to operate normally at the beginning, and always reported errors. After searching the data, I finally found a solution.
Common mistakes:
unknown type name 'sockaddr_in' shows undefined change type for two reasons, its 1 is not added
ws2_32.lib
Library, which can be added manually in project link Settings, 2 is the header file that defines only the structure name, but does not define its alias, so it cannot be used directly
sockaddr_in
To define a type
struct sockaddr_in
Definition. You can also add an alias definition
typedef struct sockaddr_in sockaddr_in
; And that's going to work.
What is truly important is invisible to the eye.
The above is C - socket programming example code details, more C - socket programming information please follow other related articles on this site!