[VC] Example of socket programming for C language in Windows (TCP and UDP)

Source: Internet
Author: User
Tags sin socket error htons

This is an example of a simple socket program, the development environment is VC6:
New Engineering win32-Console program

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 WSA
WORD sockversion = Makeword (2,2);
Wsadata Wsadata;
if (WSAStartup (sockversion, &wsadata)!=0)
{
return 0;
}

Creating sockets
Socket Slisten = socket (af_inet, sock_stream, ipproto_tcp);
if (Slisten = = Invalid_socket)
{
printf ("Socket error!");
return 0;
}

Binding IP and Ports
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 (sclient, Recdata, 255, 0);
if (Ret > 0)
{
Recdata[ret] = 0x00;
printf (recdata);
}
Closesocket (sclient);
WSACleanup ();
return 0;
}

Then the UDP
Server 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));
printf (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 (SendData), 0, (SOCKADDR *) &sin, Len);

Char recvdata[255];
int ret = Recvfrom (sclient, RecvData, 255, 0, (SOCKADDR *) &sin, &len);
if (Ret > 0)
{
Recvdata[ret] = 0x00;
printf (recvdata);
}

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.

This article is from the "Love Empty" blog, please be sure to keep this source http://an0nymous.blog.51cto.com/7744767/1634928

[VC] Example of socket programming for C language in Windows (TCP and UDP)

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.