SOCKET programming(C语言 winsock)

简介:

SOCKET programming :

1.client

basic algorithm:
(1)Find the address of the server (IP+PORT);
(2)Using SOCKET call to create the socket ;
(3)Get the local port number using CONNECT call ;
(4)Connect the client socket to the socket of the server ;(CONNECT call)
(5)Communication using some calls (write /read )
(6)Close the socket .

2.Server

Basic algorithm :
(1)create the socket ;
(2)Bind the socket to the address;
(3)Listen on the address ;
(4)Accept the client require ;
(5)EXEC some program according the client’s requirement .

Example:

// Module Name: Client.c

 // Description:

//    This sample is the echo client. It connects to the TCP server,

//    sends data, and reads data back from the server.

// Compile:

//    cl -o Client Client.c ws2_32.lib

//// Command Line Options:

//    client [-p:x] [-s:IP] [-n:x] [-o]

//           -p:x      Remote port to send to

//           -s:IP     Server's IP address or hostname

//           -n:x      Number of times to send message

//           -o        Send messages only; don't receive

 #include <winsock2.h>

#include <stdio.h>

#include <stdlib.h>

#define DEFAULT_COUNT       20

#define DEFAULT_PORT        5150

#define DEFAULT_BUFFER      2048

#define DEFAULT_MESSAGE     "This is a test of the emergency "

broadcasting system"

char szServer[128],          // Server to connect to

      szMessage[1024];        // Message to send to sever

int   iPort     = DEFAULT_PORT; // Port on server to connect to

DWORD dwCount   = DEFAULT_COUNT; // Number of times to send message

BOOL bSendOnly = FALSE;         // Send data only; don't receive

//

// Function: usage:

//

// Description:

//    Print usage information and exit

//

void usage()

{

    printf("usage: client [-p:x] [-s:IP] [-n:x] [-o]"n"n");

    printf("       -p:x      Remote port to send to"n");

    printf("       -s:IP     Server's IP address or hostname"n");

    printf("       -n:x      Number of times to send message"n");

    printf("       -o        Send messages only; don't receive"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':        // Remote port

                    if (strlen(argv[i]) > 3)

                        iPort = atoi(&argv[i][3]);

                    break;

                case 's':       // Server

                    if (strlen(argv[i]) > 3)

                        strcpy(szServer, &argv[i][3]);

                    break;

                case 'n':       // Number of times to send message

                    if (strlen(argv[i]) > 3)

                        dwCount = atol(&argv[i][3]);

                    break;

                case 'o':       // Only send message; don't receive

                    bSendOnly = TRUE;

                   break;

                default:

                    usage();

                    break;

            }

        }

    }

}

//

// Function: main

//

// Description:

//    Main thread of execution. Initialize Winsock, parse the

//    command line arguments, create a socket, connect to the

//    server, and then send and receive data.

//

int main(int argc, char **argv)

{

    WSADATA       wsd;

    SOCKET        sClient;

    char          szBuffer[DEFAULT_BUFFER];

    int           ret,

                  i;

    struct sockaddr_in server;

    struct hostent    *host = NULL;

    // Parse the command line and load Winsock

    //

    ValidateArgs(argc, argv);

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)

    {

        printf("Failed to load Winsock library!"n");

        return 1;

    }

    strcpy(szMessage, DEFAULT_MESSAGE);

    //

    // Create the socket, and attempt to connect to the server

    //

    sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (sClient == INVALID_SOCKET)

    {

        printf("socket() failed: %d"n", WSAGetLastError());

        return 1;

    }

    server.sin_family = AF_INET;

    server.sin_port = htons(iPort);

    server.sin_addr.s_addr = inet_addr(szServer);

    //

    // If the supplied server address wasn't in the form

    // "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it

    //

    if (server.sin_addr.s_addr == INADDR_NONE)

    {

        host = gethostbyname(szServer);

        if (host == NULL)

        {

            printf("Unable to resolve server: %s"n", szServer);

            return 1;

        }

        CopyMemory(&server.sin_addr, host->h_addr_list[0],

            host->h_length);

    }

    if (connect(sClient, (struct sockaddr *)&server,

        sizeof(server)) == SOCKET_ERROR)

    {

        printf("connect() failed: %d"n", WSAGetLastError());

        return 1;

    }

    // Send and receive data

    //

    for(i = 0; i < dwCount; i++)

    {

        ret = send(sClient, szMessage, strlen(szMessage), 0);

        if (ret == 0)

            break;

        else if (ret == SOCKET_ERROR)

        {

            printf("send() failed: %d"n", WSAGetLastError());

            break;

        }

        printf("Send %d bytes"n", ret);

        if (!bSendOnly)

        {

            ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);

            if (ret == 0)        // Graceful close

                break;

            else if (ret == SOCKET_ERROR)

            {

                printf("recv() failed: %d"n", WSAGetLastError());

                break;

            }

            szBuffer[ret] = '"0';

            printf("RECV [%d bytes]: '%s'"n", ret, szBuffer);

           }

    }

    closesocket(sClient);

    WSACleanup();

    return 0;

}

 

 

 

// Module Name: Server.c

// Description:

//    This example illustrates a simple TCP server that accepts

//    incoming client connections. Once a client connection is

//    established, a thread is spawned to read data from the

//    client and echo it back (if the echo option is not

//    disabled).

//

// Compile:

//    cl -o Server Server.c ws2_32.lib

//

// Command line options:

//    server [-p:x] [-i:IP] [-o]

//           -p:x      Port number to listen on

//           -i:str    Interface to listen on

//           -o        Receive only, don't echo the data back

//

#include <winsock2.h>

#include <stdio.h>

#include <stdlib.h>

#define DEFAULT_PORT        5150

#define DEFAULT_BUFFER      4096

int    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

//

// 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];

    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("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

            //

            while(nLeft > 0)

            {

                ret = send(sock, &szBuff[idx], nLeft, 0);

                if (ret == 0)

                    break;

                else if (ret == SOCKET_ERROR)

                {

                    printf("send() failed: %d"n",

                        WSAGetLastError());

                    break;

                }

                nLeft -= ret;

                idx += ret;

               }

        }

    }

    return 0;

}

//

// Function: main

//

// Description:

//    Main thread of execution. Initialize Winsock, parse the

//    command line arguments, create the listening socket, bind

//    to the local address, and wait for client connections.

//

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, 8);

    //

    // In a continous loop, wait for incoming clients. Once one

    // is detected, create a thread and pass the handle off to it.

    //

    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;

}

本文转自BlogJava 新浪blog的博客,原文链接SOCKET programming(C语言 winsock):,如需转载请自行联系原博主。

相关文章
|
2月前
|
存储 网络协议 安全
C语言 网络编程(五)Socket和端口
Socket 是 TCP/IP 五层网络模型中应用层的编程接口,用于实现不同主机间应用程序的双向通信。它作为网络通信的端点,连接应用层与网络协议栈,提供可靠的流式或非流式数据传输服务。Socket 包括流式(SOCKET_STREAM)、数据报(SOCK_DGRAM)和原始套接字(SOCK_RAW)三种类型,分别适用于不同场景。通过 IP 地址和端口号,Socket 能准确识别并转发数据包至指定进程。端口号分为知名端口(1-1023)、注册端口(1024-49151)和动态端口(49152-65535),确保数据准确交付。
|
网络协议 C语言 C++
【Socket】C语言编写Web服务器
【Socket】C语言编写Web服务器
|
网络协议 编译器 C语言
c语言实现TCP的socket通信
c语言实现TCP的socket通信
|
存储 Java Linux
纯C语言Socket实现聊天室
最近在学习嵌入式开发,练习C语言小项目,基本是参考别人的代码,做了些修改实现了聊天室,纯C语言编写。
305 0
纯C语言Socket实现聊天室
|
C语言
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
145 0
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
|
C语言
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的本地纹理+FBO+VBO”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的本地纹理+FBO+VBO”示例代码【C语言版】
121 0
《OpenGL ES 2.0 Programming Guide》第12章 “最简单的本地纹理+FBO+VBO”示例代码【C语言版】
|
C语言
《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理+VBO”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理+VBO”示例代码【C语言版】
153 0
《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理+VBO”示例代码【C语言版】
|
C语言 Android开发 C++
《OpenGL ES 2.0 Programming Guide》第12章“最简单的FBO Depth Texture”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第12章“最简单的FBO Depth Texture”示例代码【C语言版】
178 0
《OpenGL ES 2.0 Programming Guide》第12章“最简单的FBO Depth Texture”示例代码【C语言版】
|
C语言
《OpenGL ES 2.0 Programming Guide》第11章“最简单的MSAA”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第11章“最简单的MSAA”示例代码【C语言版】
263 0
《OpenGL ES 2.0 Programming Guide》第11章“最简单的MSAA”示例代码【C语言版】
|
C语言
《OpenGL ES 2.0 Programming Guide》第9章“最简单的MipMap”示例代码【C语言版】
《OpenGL ES 2.0 Programming Guide》第9章“最简单的MipMap”示例代码【C语言版】
178 0
《OpenGL ES 2.0 Programming Guide》第9章“最简单的MipMap”示例代码【C语言版】
下一篇
无影云桌面