C Language Socket Programming Development application Example _c language

Source: Internet
Author: User
Tags int size sleep function htons



Implementation steps:

1. Server End

Copy Code code as follows:

#include <stdio.h>//calls for functions such as printf
function calls for #include <winsock2.h>//socket
#pragma comment (lib, "ws2_32")//C language References other class libraries, in addition to the. h file, add the corresponding lib file (this is different from C #)

Copy Code code as follows:

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 IP address is required to bind to the local
Sockaddr.sin_port=htons (9000); Ports that need to be tapped
Bind (S, (sockaddr*) &sockaddr, sizeof (SOCKADDR)); Make 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 a new TCP client connection is available
printf ("***sys*** New client touched.\n");

char* msg= "Hello, my client.\r\n";
Send (Clientsocket, MSG, strlen (msg) +sizeof (char), NULL); The third parameter here is to note that a char length is added to the
printf ("***sys*** hello.\n");

while (TRUE)
{
Char buffer[maxbyte]={0};
Recv (clientsocket, buffer, maxbyte, NULL); Receive send operation for client socket all the time
printf ("***client***%s\n", buffer);
}

Closesocket (Clientsocket); Close socket
}

Closesocket (s); Turn off listening sockets

WSACleanup (); Unloading

GetChar ();

Exit (0);
}

Because it is still learning, so there is no multithreading, so the client can actually only 1, the others will be blocked outside

About the socket buffer is also a bit learned, such as the end of the end of the \ r \ n, and so on, there are reasons, suggest to look at the data.

2. Client Side

Copy Code code as follows:

#include <stdio.h>//call for input and output functions, printf, gets
#include <winsock2.h>//socket header files
#include <Windows.h>//In order to facilitate debugging, so joined the wait 2 seconds to connect to the server, where the sleep function

#pragma comment (lib, "ws2_32")//socket library file

Copy Code code as follows:

int main ()
{
Sleep (2000); Sleep for 2 seconds and connect to the 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=new char[100000];
printf ("Can chat with server now:\n");
Gets (mymsg);
Send (S, mymsg, strlen (mymsg) +sizeof (char), NULL);
/*
The Bufferlength parameter in the RECV function can be fixed to a value
The Bufferlength parameter in the Send function cannot be fixed to a value, it needs to look at the actual length and takes into account the ' I ' string
*/
}

Closesocket (s);

WSACleanup ();

GetChar ();

Exit (0);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.