码迷,mamicode.com
首页 > 编程语言 > 详细

【Socket编程】套接字Windows平台C语言实现

时间:2014-10-12 19:26:38      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:c语言   socket   套接字   网络编程   tcp   

【编译环境】:Visual Studio 2013

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

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

const int BACK_LOG = 5;

int main(int argc, char * argv[])
{
	//初始化套接字
	WSADATA wsaData;
	int ret = WSAStartup(MAKEWORD(2, 4), &wsaData);
	if (0 != ret){
		fputs("WSAStartup error\n", stderr);
		return 1;
	}

	//创建套接字
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sock) {
		fputs("socket() error\n", stderr);
		WSACleanup();
		return 1;
	}

	//绑定套接字
	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(SOCKADDR_IN));
	sockAddr.sin_family = AF_INET;//协议簇,一般用AF_INET表示TCP/IP协议
	sockAddr.sin_addr.S_un.S_addr = htonl(ADDR_ANY);//表示IP地址
	sockAddr.sin_port = htons(7);//表示端口

	ret = bind(sock, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
	if (SOCKET_ERROR == ret) {
		fputs("bind() error\n", stderr);
		closesocket(sock);
		WSACleanup();
		return 1;
	}

	//侦听套接字
	ret = listen(sock, BACK_LOG);
	if (SOCKET_ERROR == ret) {
		fputs("listen() error\n", stderr);
		closesocket(sock);
		WSACleanup();
		return 1;
	}

	//接收套接字
	SOCKADDR_IN clientAddr;
	int szClientAddr = sizeof(clientAddr);

	SOCKET clientSock = accept(sock, (SOCKADDR*)&clientAddr, &szClientAddr);

	if (INVALID_SOCKET == clientSock){
		fputs("accept() error\n", stderr);
		closesocket(sock);
		WSACleanup();
		return 1;
	}

	//收发数据
	TCHAR buff[256];
	memset(&buff,0,sizeof(buff));

	recv(sock,buff,sizeof(buff),0);//recv()返回读入的字节数

	TCHAR buff2[] = "QUIT\n";
	send(sock, buff2, sizeof(buff2), 0);//成功则返回实际传送出去的字符数

	//回收资源
	closesocket(sock);
	closesocket(clientSock);
	WSACleanup();
	
	return 0;

}


【Socket编程】套接字Windows平台C语言实现

标签:c语言   socket   套接字   网络编程   tcp   

原文地址:http://blog.csdn.net/lu_chaoqun/article/details/40018743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!