kken0206

VC6 tcp udp 通信例子。

windows下socket开发tcp程序 简单例子 感谢网友 RobinTang.. windows下socket开发udp程序

本机的TCP和UDP通信.VC6上可正常运行!

1 TCP server
//TCP server
//listen port 9102
//receive string and display it

//Visual C++ 6.0

#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")


#define BUFLEN 1024

int main()
{
SOCKET serversoc;
SOCKET clientsoc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
char buf[BUFLEN];
int len;

WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa); //initial Ws2_32.dll by a process
if((serversoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) //create a tcp socket
{
printf("Create socket fail!\n");
return -1;
}

serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

if(bind(serversoc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Bind fail!\n");
return -1;
}

//start listen, maximum length of the queue of pending connections is 1
printf("Start listen...\n");
if(listen(serversoc, 1) != 0)
{
printf("Listen fail!\n");
return -1;
}

len = sizeof(SOCKADDR_IN);


//waiting for connecting
if((clientsoc = accept(serversoc, (SOCKADDR *)&clientaddr, &len))<=0)
{
printf("Accept fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
//waiting for data receive
if(recv(clientsoc, buf, BUFLEN, 0) <= 0)
{
//some error occur
printf("Close connection\n");
closesocket(clientsoc);
break;
}
printf("%s\n",buf);
}

WSACleanup(); //clean up Ws2_32.dll
return 0;
}


2 tCP_client

//TCP client
//client send string to server

//Visual C++ 6.0

#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")


int main()
{
SOCKET soc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
unsigned char buf[1024];

WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa); //initial Ws2_32.dll by a process

if((soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) //create a tcp socket
{
printf("Create socket fail!\n");
return -1;
}

serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

//connect to server
printf("Try to connect...\n");
if(connect(soc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Connect fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
scanf("%s", buf);
//send to server
if(send(soc, buf, strlen(buf)+1, 0)<=0)
{
printf("Error!\n");
}
}

WSACleanup(); //clean up Ws2_32.dll
return 0;
}

3 UDP_server

//UDP server
//listen port 9102
//receive string and display it

//Visual C++ 6.0

#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

#define BUFLEN 1024

int main(void)
{
SOCKET soc;
SOCKADDR_IN addr;
char buf[BUFLEN];
int len;

WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa); //initial Ws2_32.dll by a process

memset(&addr, 0, sizeof(addr));

if((soc = socket(AF_INET,SOCK_DGRAM,0)) <= 0)
{
printf("Create socket fail!\n");
return -1;
}


addr.sin_family = AF_INET;
addr.sin_port = htons(9102);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(soc,(struct sockaddr *)&addr,sizeof(struct sockaddr))!=0)
{
printf("Bind fail!\n");
return -1;
}


len = sizeof(addr);
printf("start listen...\n");
while(1) {
recvfrom(soc, buf, BUFLEN, 0,(struct sockaddr*)&addr, &len);
printf("%s\n",buf);
}

WSACleanup(); //关闭
return 0;
}

4 UDPclient

//UDP client
//client send string to server

//Visual C++ 6.0

#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

#define BUFLEN 1024

int main(void)
{
SOCKET soc;
SOCKADDR_IN addr;
unsigned char buf[BUFLEN];

WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa); //initial Ws2_32.dll by a process

memset(&addr, 0, sizeof(addr));


if((soc = socket(AF_INET,SOCK_DGRAM,0)) <= 0)
{
printf("Create socket fail!\n");
return -1;
}

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(9102);
bind(soc,(struct sockaddr *)&addr,sizeof(addr));


while(1) {
scanf("%s", buf);
sendto(soc, buf, strlen(buf)+1, 0, (struct sockaddr *)&addr, sizeof(addr));
}

WSACleanup(); //clean up Ws2_32.dll
return 0;
}