Socket programming instance in UDP windows

Source: Internet
Author: User

Source code of the server program:
# Include <stdio. h>
# Include <winsock2.h>
Void main ()
{
Socket socket1;
// Initwinsock ();
Wsadata;
Int ierrorcode;
If (wsastartup (makeword (2, 1), & wsadata) // call Windows Sockets DLL
{
Printf ("Winsock cannot be initialized! \ N ");
Wsacleanup ();
Return;
}
Printf ("the server starts to create a socket. \ N ");
Struct sockaddr_in local;
Struct sockaddr_in from;
Int fromlen = sizeof (from );
Local. sin_family = af_inet;
Local. sin_port = htons (27015); // listener Port
Local. sin_addr.s_addr = inaddr_any; // Local Machine
Socket1 = socket (af_inet, sock_dgram, 0 );
BIND (socket1, (struct sockaddr *) & Local, sizeof (local ));
While (1)
{
Char buffer [1024] = "\ 0 ";
Printf ("waiting for message from others ------------- \ n ");
If (recvfrom (socket1, buffer, sizeof (buffer), 0, (struct sockaddr *) & from, & fromlen )! = Socket_error)
{
Printf ("received datainfrom % s -- % s \ n", inet_ntoa (from. sin_addr), buffer );
//// Send a message to cilent
Sendto (socket1, buffer, sizeof (buffer), 0, (struct sockaddr *) & from, fromlen );
}
// Sleep (1, 1000 );
}
Closesocket (socket1 );
Wsacleanup ();
}
Client source code:
# Include <stdio. h>
# Include <winsock2.h>
Void main ()
{
Socket socket1;
// Initwinsock ();
Wsadata;
Int ierrorcode;
If (wsastartup (makeword (2, 1), & wsadata) // call Windows Sockets DLL
{
Printf ("Winsock cannot be initialized! \ N ");
Wsacleanup ();
Return;
}
Printf ("the server starts to create a socket. \ N ");
Struct sockaddr_in server;
Int Len = sizeof (server );
Server. sin_family = af_inet;
Server. sin_port = htons (27015); // listener port of the server
Server. sin_addr.s_addr = inet_addr ("221.223.16.32"); // server address
Socket1 = socket (af_inet, sock_dgram, 0 );
While (1)
{
Char buffer [1024] = "\ 0 ";
Printf ("input message \ n ");
Scanf ("% s", buffer );
// Printf ("% s", buffer );
If (strcmp (buffer, "bye") = 0)
{
Printf ("quit! \ N ");
Sleep (100 );
Closesocket (socket1 );
Break;
}
If (sendto (socket1, buffer, sizeof (buffer), 0, (struct sockaddr *) & server, Len )! = Socket_error)
{
// Printf ("sending .. \ n ");
Sleep (100 );
If (recvfrom (socket1, buffer, sizeof (buffer), 0, (struct sockaddr *) & server, & Len )! = Socket_error)
Printf ("rece from server: % s \ n", buffer );
}
}
Closesocket (socket1 );
Wsacleanup ();

}

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.