| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1146 人关注过本帖
标题:写了一个简单的socket客户端/服务器程序, 放在了github上
只看楼主 加入收藏
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
结帖率:94.72%
收藏
 问题点数:0 回复次数:1 
写了一个简单的socket客户端/服务器程序, 放在了github上
程序代码:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int mySocket = socket(AF_INET, SOCK_STREAM, 0);
    
    struct hostent *host_entry = gethostbyname("localhost");
    
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(7640);
    server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
    connect(mySocket, (struct sockaddr*)&server, sizeof(server));
    
#define MESSAGE_SIZE 24
    char inMessage[MESSAGE_SIZE] = {0};
    char outMessage[MESSAGE_SIZE] = "I sent you this message";
    
    int nBytes = send(mySocket, outMessage, sizeof(outMessage), 0);
    printf("Message Sent : %s\n", outMessage);
    
    nBytes = recv(mySocket, inMessage, sizeof(inMessage), 0);
    printf("Message Received : %s\n", inMessage);
    
    close(mySocket);
    return 0;
}

程序代码:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(7640);
    server.sin_addr.s_addr = INADDR_ANY;
    bind(serverSocket, (struct sockaddr*)&server, sizeof(server));
    
    listen(serverSocket, 5);
    int clientSocket = accept(serverSocket, 0, 0);
    
#define MESSAGE_SIZE 24
    char inMessage[MESSAGE_SIZE] = {};
    char outMessage[MESSAGE_SIZE] = "I received your message";
    
    int nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0);
    printf("Message Received : %s\n", inMessage);
    
    nBytes = send(clientSocket, outMessage, sizeof(outMessage), 0);
    printf("Message Sent : %s\n", outMessage);
    
    close(clientSocket);
    close(serverSocket);
    
    return 0;
}


[此贴子已经被作者于2015-12-19 00:36编辑过]

搜索更多相关主题的帖子: 服务器 客户端 
2015-12-17 22:39
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
程序代码:
#include <winsock2.h>
#include <stdio.h>

int main()
{
    WSAData wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    int serverSocket = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(7654);
    server.sin_addr.s_addr = INADDR_ANY;

    bind(serverSocket, (sockaddr*)&server, sizeof(server)); 

    listen(serverSocket, 5);

    int clientSocket = accept(serverSocket, 0, 0);

    #define MESSAGE_SIZE 24
    char inMessage[MESSAGE_SIZE];
    char outMessage[MESSAGE_SIZE] = "I received your message";
    
    int nBytes = recv(clientSocket, inMessage, sizeof(inMessage), 0);
    printf("Message Received : %s\n", inMessage);

    nBytes = send(clientSocket, outMessage, sizeof(outMessage), 0);
    printf("Message Sent : %s\n", outMessage);
    
    getchar();
    return 0;
}
程序代码:
#include <winsock2.h>
#include <stdio.h>

int main() 
{
    WSAData wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    int mySocket = socket(AF_INET, SOCK_STREAM, 0);
    
    struct hostent *host_entry = gethostbyname("localhost");
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(7654);
    server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
    
    connect(mySocket, (sockaddr*)&server, sizeof(server));

#define MESSAGE_SIZE 24
    char inMessage[MESSAGE_SIZE];
    char outMessage[MESSAGE_SIZE] = "I sent you this message";

    int nBytes = send(mySocket, outMessage, sizeof(outMessage), 0);
    printf("Message Sent : %s\n", outMessage);

    nBytes = recv(mySocket, inMessage, sizeof(inMessage), 0);
    printf("Message Received : %s\n", inMessage);

    getchar();
    return 0;
}


[此贴子已经被作者于2015-12-19 00:37编辑过]


我就是真命天子,顺我者生,逆我者死!
2015-12-17 22:39
快速回复:写了一个简单的socket客户端/服务器程序, 放在了github上
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017517 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved