Text: Socket Programming example for C in Windows (TCP and UDP)
just learned Windows programming, so want to write study notes, this is a simple socket program example, the development environment is VC6: the first is TCP Server side:
#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") int main (int ARGC, char* argv[]) {//Initialize Wsaword sockversion = Makeword (2,2); Wsadata wsadata;if (WSAStartup (sockversion, &wsadata)!=0) {return 0;} Create socket Socket Slisten = socket (af_inet, Sock_stream, ipproto_tcp), if (Slisten = = Invalid_socket) {printf ("Socket error!"); return 0;} Bind IP and port sockaddr_in sin;sin.sin_family = Af_inet;sin.sin_port = htons (8888); sin.sin_addr. S_un. S_ADDR = Inaddr_any; if (Bind (Slisten, (lpsockaddr) &sin, sizeof (sin)) = = Socket_error) {printf ("Bind ERROR!");} Start listening if (Listen (Slisten, 5) = = Socket_error) {printf ("Listen ERROR!"); return 0;} Loop receive data socket sclient;sockaddr_in remoteaddr;int naddrlen = sizeof (REMOTEADDR); char revdata[255]; while (true) {printf ("Wait for connection ... \ n"); sclient = Accept (Slisten, (SOCKADDR *) &remoteaddr, &naddrlen); if (sclient = = Invalid_socket) {printf ("Accept error!"); Continue;} printf ("Accepted to a connection:%s \ r \ n", Inet_ntoa (REMOTEADDR.SIN_ADDR));//Receive data INT ret = recv (sclient, Revdata, 255, 0); if (Ret > 0) {Revdata[ret] = 0x00;printf (revdata);} Send Data char * senddata = "Hello, TCP client!" \ n "; Send (Sclient, SendData, strlen (SendData), 0); closesocket (sclient);} Closesocket (Slisten); WSACleanup (); return 0;}
Client side:
#include "stdafx.h" #include <winsock2. H> #include <stdio. h> #pragma comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {WORD sockversion = Makeword (2,2); Wsadata data; if (WSAStartup (sockversion, &data)! = 0) {return 0;} Socket sclient = socket (af_inet, sock_stream, ipproto_tcp); if (sclient = = Invalid_socket) {printf ("INVALID socket!"); return 0;} sockaddr_in seraddr;seraddr.sin_family = Af_inet;seraddr.sin_port = htons (8888); seraddr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); if (Connect (sclient, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Connect ERROR!"); Closesocket (sclient); return 0;} char * senddata = "Hello, tcp server, I am client!\n"; Send (Sclient, SendData, strlen (SendData), 0); char recdata[255];int ret = recv (scli ENT, Recdata, 255, 0); if (Ret > 0) {Recdata[ret] = 0x00;printf (recdata);} Closesocket (sclient); WSACleanup (); return 0;}
then the UDPServer side:
#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") int main (int ARGC, char* argv[]) {wsadata wsadata; WORD sockversion = Makeword (2,2); if (WSAStartup (sockversion, &wsadata)! = 0) {return 0;} Socket sersocket = socket (af_inet, SOCK_DGRAM, IPPROTO_UDP); if (Sersocket = = Invalid_socket) {printf ("SOCKET error!"); return 0;} sockaddr_in seraddr;seraddr.sin_family = Af_inet;seraddr.sin_port = htons (8888); seraddr.sin_addr. S_un. S_ADDR = Inaddr_any;if (Bind (Sersocket, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Bind ERROR!") ; closesocket (sersocket); return 0;} sockaddr_in remoteaddr;int naddrlen = sizeof (REMOTEADDR); while (true) {char recvdata[255]; int ret = Recvfrom (Sersocket, RecvData, 255, 0, (SOCKADDR *) &remoteaddr, &naddrlen); if (Ret > 0) {Recvdata[ret] = 0x00;printf ("Accepted to a connection:%s \ r \ n", Inet_ntoa (remoteaddr.sin_addr));p rintf (recvdata);} char * senddata = "A UDP packet from the server \ n"; sendto (Sersocket, SendData, strlen (SENddata), 0, (SOCKADDR *) &remoteaddr, naddrlen);} Closesocket (Sersocket); WSACleanup (); return 0;}
Client side:
#include "stdafx.h" #include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") int main (int ARGC, char* argv[]) {WORD socketversion = Makeword (2,2); Wsadata Wsadata; if (WSAStartup (socketversion, &wsadata)! = 0) {return 0;} Socket sclient = socket (af_inet, SOCK_DGRAM, ipproto_udp); sockaddr_in sin;sin.sin_family = Af_inet;sin.sin_port = Htons ( 8888); sin.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); int len = sizeof (SIN), char * senddata = "packet from client. \ n"; sendto (Sclient, SendData, strlen (s Enddata), 0, (SOCKADDR *) &sin, Len); char recvdata[255]; int ret = Recvfrom (sclient, RecvData, 255, 0, (SOCKADDR *) &sin, &len); if (Ret > 0) {Recvdata[ret] = 0x00;printf (r Ecvdata);} Closesocket (sclient); WSACleanup (); return 0;}
test can be passed, is not aware of those areas can be improved, those places can be deleted, if someone knows can give me a message. http://shop110737039.taobao.com/
Example of socket programming for C language in Windows (TCP and UDP)