c语言写的tcp协议Server

  • j1_656258
    了解作者
  • 19.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-16 01:13
    上传日期
c语言写的tcp协议Server控制台程序
tserver.zip
  • tserver
  • resource.h
    386B
  • cserver.opt
    55.5KB
  • server.cpp
    5.6KB
  • cserver.vcproj
    4.4KB
  • cserver.suo
    8.5KB
  • cserver.sln
    903B
  • wsock32.lib
    0B
  • cserver.plg
    1.2KB
  • cserver.aps
    17.1KB
  • cserver.rc
    1.3KB
内容介绍
#include <winsock.h> //#include <winbase.h> #include <stdio.h> #include <stdlib.h> #define DEFAULT_PORT 7878 #define DEFAULT_BUFFER 4096 unsigned short iPort = DEFAULT_PORT; // Port to listen for clients on BOOL bInterface = FALSE, // Listen on the specified interface bRecvOnly = FALSE; // Receive data only; don't echo back char szAddress[128]; // Interface to listen for clients on void usage(void); void strsub(char *str1,char *str2,int i,int j); // Function: usage Description: Print usage information and exit void usage() { printf("usage: server [-p:x] [-i:IP] [-o]\n\n"); printf(" -p:x Port number to listen on\n"); printf(" -i:str Interface to listen on\n"); printf(" -o Don't echo the data back\n\n"); ExitProcess(1); } // Function: ValidateArgs // Description: Parse the command line arguments, and set some global flags to indicate what actions to perform void ValidateArgs(int argc, char **argv) { int i; for(i = 1; i < argc; i++) { if ((argv[i][0] == '-') || (argv[i][0] == '/')) { switch (tolower(argv[i][1])) { case 'p': iPort = atoi(&argv[i][3]); break; case 'i': bInterface = TRUE; if (strlen(argv[i]) > 3) strcpy(szAddress, &argv[i][3]); break; case 'o': bRecvOnly = TRUE; break; default: usage(); break; } } } } // Function: ClientThread // // Description: // This function is called as a thread, and it handles a given // client connection. The parameter passed in is the socket // handle returned from an accept() call. This function reads // data from the client and writes it back. DWORD WINAPI ClientThread(LPVOID lpParam) { SOCKET sock=(SOCKET)lpParam; char szBuff[DEFAULT_BUFFER]; char reBuff[DEFAULT_BUFFER]; int ret, nLeft, idx; while(1) { // Perform a blocking recv() call ret = recv(sock, szBuff, DEFAULT_BUFFER, 0); if (ret == 0) // Graceful close break; else if (ret == SOCKET_ERROR) { printf("recv() failed: %d\n", WSAGetLastError()); break; } szBuff[ret] = '\0'; printf("len:[%d]\n", ret); printf("RECV: '%s'\n", szBuff); // If we selected to echo the data back, do it // if (!bRecvOnly) { nLeft = ret; idx = 0; // // Make sure we write all the data strcpy(reBuff,szBuff); nLeft = strlen(reBuff); idx = 0; while(nLeft > 0) { Sleep(1000); ret = send(sock, &reBuff[idx], nLeft, 0); if (ret == 0) break; else if (ret == SOCKET_ERROR) { printf("send() failed: %d\n", WSAGetLastError()); break; } printf("Send: '%s'\n", reBuff); nLeft -= ret; idx += ret; } } } return 0; } int main(int argc, char **argv) { WSADATA wsd; SOCKET sListen, sClient; int iAddrSize; HANDLE hThread; DWORD dwThreadId; struct sockaddr_in local, client; ValidateArgs(argc, argv); if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { printf("Failed to load Winsock!\n"); return 1; } // Create our listening socket sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sListen == SOCKET_ERROR) { printf("socket() failed: %d\n", WSAGetLastError()); return 1; } // Select the local interface and bind to it if (bInterface) { local.sin_addr.s_addr = inet_addr(szAddress); if (local.sin_addr.s_addr == INADDR_NONE) usage(); } else local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_family = AF_INET; local.sin_port = htons(iPort); if (bind(sListen, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR) { printf("bind() failed: %d\n", WSAGetLastError()); return 1; } listen(sListen, 5); printf("listen port: %d\n", iPort); while (1) { iAddrSize = sizeof(client); sClient = accept(sListen, (struct sockaddr *)&client, &iAddrSize); if (sClient == INVALID_SOCKET) { printf("accept() failed: %d\n", WSAGetLastError()); break; } printf("Accepted client: %s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port)); hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)sClient, 0, &dwThreadId); if (hThread == NULL) { printf("CreateThread() failed: %d\n", GetLastError()); break; } CloseHandle(hThread); } closesocket(sListen); WSACleanup(); return 0; } void strsub(char *str1,char *str2,int i,int j) { i=i-1; int p=0; do{ str2[p++]=str1[i++]; }while(i<j); str2[p]='\0'; }
评论
    相关推荐
    • 谭浩强C语言
      这是谭浩强C语言的新版,有兴趣想学C语言的童鞋们可以下载来看看啊!
    • C语言 实现
      C语言 项目实现 《计算方法》课件 俄罗斯方块游戏 C语言实训 综合案例-学生成绩管理程序 C语言程序设计学习与实践指导(源代码)
    • 水滴石穿C语言
      学习C语言的有用文档 水滴石穿C语言C语言的底层操作 水滴石穿C语言之extern声明辨析 水滴石穿C语言之static辨析 水滴石穿C语言之typedef的问题 水滴石穿C语言之编译器引出的问题 水滴石穿C语言之代码检查工具 ...
    • C语言精讲
      初学者必看,让你轻松学会c语言,课程详细,内容丰富
    • 谭浩强c语言
      谭浩强c语言,国内最权威的c语言学习宝典,从零基础开始,成为c语言高手。
    • Makefile c语言
      Makefile c语言Makefile c语言Makefile c语言Makefile c语言 四本资料!自己学习的时候整理的!
    • C语言 课件
      此文件是小甲鱼c语言的 第四章 分支结构程序(课件).rar )
    • c语言
      C语言电子课件 简单易懂 清楚明了 希望让每一个学它的人都有所收获 。
    • c语言
      c语言
    • C语言
      C语言