两台电脑间通过网络传输数据

everover 2014-10-15 04:29:01
小白来问一个最基础的问题,局域网内的两台电脑间如何传输数据(比如一台电脑发送字符串“Hello”,另一台接收),由于没接触过,无从下手,最好把代码贴上,发送端和接收端都要,谢谢啦
...全文
375 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
sichuanwww 2014-11-04
  • 打赏
  • 举报
回复
wanglovec 2014-11-04
  • 打赏
  • 举报
回复
找 windows 网络编程 看看 什么都会了, 看不懂就搜小猪的教程 讲的很详细 有代码有真想。
jianghandaxue 2014-11-03
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390257815
jianghandaxue 2014-11-03
  • 打赏
  • 举报
回复
聊天程序C/S,网上一对吧的DEMO源码。100分 好多哇!
Marcelxx 2014-10-16
  • 打赏
  • 举报
回复
百度一下UDP客户端和服务端,和TCP客户端和服务端,有很多的示例代码。估计Lz是在VS中调式代码,所以下载windows的客户端和服务端。Ls有个贴的代码时Linux下的。
精分患者 2014-10-16
  • 打赏
  • 举报
回复
引用 9 楼 modyaj 的回复:
你来发个贴 用了100 分 你要是用100换成资源分 是20个资源分 一份代码 好像最多10个资源分 你能下载至少两份! 而且不是每个资源都要十分 也不是每个资源都要分的! 最后的问题是,照着网络编程的那几个步骤走一下,要不了多少时间!
难道能换?????
modyaj 2014-10-16
  • 打赏
  • 举报
回复
你来发个贴 用了100 分 你要是用100换成资源分 是20个资源分 一份代码 好像最多10个资源分 你能下载至少两份! 而且不是每个资源都要十分 也不是每个资源都要分的! 最后的问题是,照着网络编程的那几个步骤走一下,要不了多少时间!
版主大哥 2014-10-16
  • 打赏
  • 举报
回复

tcpclient:
 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
 
#define HOST_PORT               10000
int main()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return;
    }
 
    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */
 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 2 )
    {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return;
    }
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd < 0)
    {
        printf("client create socket failed.\n");
        return 0;
    }
    int on = 1;
    if(setsockopt(clientfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
    {
        printf("set socket option failed.\n");
        return 0;
    }
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
 
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(HOST_PORT);
    servaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.56");
 
    char buf[BUFSIZ];
    memset(buf,0,BUFSIZ);
    strncpy(buf,"shawn's client",14);
    //buf[BUFSIZ] = '\0';
 
    if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        printf("%d\n",errno);
        return 0;
    }
    int index = 0;
    for(index = 0; index < 5; index++)
    {
        send(clientfd,buf,strlen(buf) + 1, 0);
        _sleep(10);
    }
 
    memset(buf,0,BUFSIZ);
    strcpy(buf,"end");
    send(clientfd,buf,strlen(buf) + 1, 0);
    closesocket(clientfd);
 
    getchar();
    return 0;
}
tcpserver.c:
 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
 
#define MAX_LISTEN_QUEUE_NUMBER                 10
#define HOST_PORT                               10000
#define MAX_PACKET_LENGTH                       1400
int main()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return 0;
    }
 
    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */
 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 2 )
    {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return 0;
    }
 
    int servfd, clientfd;
    servfd = socket(AF_INET, SOCK_STREAM, 0);
    if(servfd < 0)
    {
        printf("server create socket failed.\n");
        return 0;
    }
    int on = 1;
    if(setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
    {
        printf("set socket option failed.\n");
        return 0;
    }
    struct sockaddr_in servaddr;
    struct sockaddr_in clientaddr;
 
    memset(&servaddr,0,sizeof(servaddr));
    memset(&clientaddr,0,sizeof(clientaddr));
 
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(HOST_PORT);
    servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    if(bind(servfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        printf("bind failed.\n");
        return 0;
    }
    if(listen(servfd,10) < 0)
    {
        printf("listen failed.\n");
        return 0;
    }
    char buf[MAX_PACKET_LENGTH];
    memset(buf,0,MAX_PACKET_LENGTH);
    int length = sizeof(clientaddr);
 
    int clientRecord[FD_SETSIZE];
    int index = 0;
    for(; index < FD_SETSIZE; index++)
        clientRecord[index] = -1;
    fd_set readSet;
    FD_ZERO(&readSet);
    FD_SET(servfd,&readSet);
    int maxfd = servfd;
 
    for(;;)
    {
        struct timeval time;
        time.tv_sec = 10;
        time.tv_usec = 0;
        if(select(maxfd,&readSet,NULL,NULL,&time) <= 0)
            continue;
        if(FD_ISSET(servfd,&readSet))
        {
            clientfd = accept(servfd, (struct sockaddr *)&clientaddr,&length);
 
            for(index = 0; index < FD_SETSIZE; index++)
            {
                if(clientRecord[index] < 0)
                {
                    clientRecord[index] = clientfd;
                    break;
                }
            }
            if(index == FD_SETSIZE)
            {
                printf("too many sockets.\n");
                break;
            }
        }
        for(index = 0; index < FD_SETSIZE; index++)
        {
            if(clientRecord[index] > 0)
            {
                FD_SET(clientRecord[index],&readSet);
                maxfd = maxfd > clientRecord[index] ? maxfd : clientRecord[index];
            }
            else
                break;
        }
 
        int sockfd;
 
        for(index = 0; index < FD_SETSIZE; index++)
        {
            if((sockfd = clientRecord[index]) < 0)
                continue;
            if(FD_ISSET(sockfd,&readSet))
            {
                while(1)
                {
                    recv(sockfd,buf,MAX_PACKET_LENGTH,0);
                    printf("%s\n",buf);
                    if(strcmp(buf,"end") == 0)
                        break;
                }
                closesocket(sockfd);
                clientRecord[index] = -1;
            }
        }
    }
    closesocket(servfd);
    return 0;
}
版主大哥 2014-10-16
  • 打赏
  • 举报
回复

    //tcpserver.cc  
    #include <iostream>  
    #include <cstring>  
    #include <strings.h>  
    #include <stdlib.h>  
    #include <sys/socket.h>  
    #include <netinet/in.h>  
    #include <arpa/inet.h>  
    using namespace std;  
      
    int main(int argc, char *argv[])  
    {  
        //创建套接字  
        int sk = socket(AF_INET, SOCK_STREAM, 0);  
      
        struct sockaddr_in server;  
        bzero(&server, sizeof(server));  
        server.sin_family = AF_INET;  
        server.sin_port = htons(atoi(argv[1]));  
        server.sin_addr.s_addr = htonl(INADDR_ANY);  
        //端口绑定  
        bind(sk, (struct sockaddr*)&server, sizeof(server));  
      
        //监听  
        listen(sk, 5);  
      
        struct sockaddr_in client;  
        bzero(&client, sizeof(client));  
        size_t len = sizeof(client);  
        //接受连接请求  
        int talk = accept(sk, (struct sockaddr*)&client, &len);  
      
        //发送数据  
        send(talk, "Hello", strlen("Hello") + 1, 0);  
        //接收数据  
        char buff[1024] = {'\0'};  
        recv(talk, buff, sizeof(buff), 0);  
        cout << buff << endl;  
      
        //关闭套接字  
        close(talk);  
        close(sk);  
        return 0;  
    }  



    //tcpclient.cc  
    #include <iostream>  
    #include <cstring>  
    #include <strings.h>  
    #include <stdlib.h>  
    #include <sys/socket.h>  
    #include <arpa/inet.h>  
    #include <netinet/in.h>  
    using namespace std;  
      
    int main(int argc, char *argv[])  
    {  
        //创建套接字  
        int sk = socket(AF_INET, SOCK_STREAM, 0);  
      
        struct sockaddr_in server;  
        server.sin_family = AF_INET;  
        server.sin_port = htons(atoi(argv[2]));  
        server.sin_addr.s_addr = inet_addr(argv[1]);  
        //连接服务器  
        connect(sk, (struct sockaddr*)&server, sizeof(server));  
      
        char buff[1024] = {'\0'};  
        //接收数据  
        recv(sk, buff, sizeof(buff), 0);  
        cout << buff << endl;  
        //发送数据  
        send(sk, "I am hahaya", strlen("I am hahaya") + 1, 0);  
      
        //关闭套接字  
        close(sk);  
        return 0;  
      
      
    }  
lx624909677 2014-10-16
  • 打赏
  • 举报
回复
学一下socket编程
xian_wwq 2014-10-16
  • 打赏
  • 举报
回复
http://blog.csdn.net/woshinia/article/details/8585930 看Windows Socket 五种IO模型,从易到难都有了
modyaj 2014-10-16
  • 打赏
  • 举报
回复
论坛-》我的论坛-》下载分兑换
modyaj 2014-10-16
  • 打赏
  • 举报
回复
引用 10 楼 Peter_Hugh 的回复:
引用 9 楼 modyaj 的回复:
你来发个贴 用了100 分 你要是用100换成资源分 是20个资源分 一份代码 好像最多10个资源分 你能下载至少两份! 而且不是每个资源都要十分 也不是每个资源都要分的! 最后的问题是,照着网络编程的那几个步骤走一下,要不了多少时间!
难道能换?????
能换啊!5:1的兑换方式
笨笨仔 2014-10-15
  • 打赏
  • 举报
回复
把分给我,留个Mail我发个自己写的TCP通信平台的DEMO给你。
everover 2014-10-15
  • 打赏
  • 举报
回复
引用 2 楼 shenyi0106 的回复:
100分好贵啊
太简单了,没人回答了
everover 2014-10-15
  • 打赏
  • 举报
回复
引用 1 楼 piaobotudou 的回复:
网上随便搜索一下有很多。http://download.csdn.net/detail/xuyunhou/3166403
刚注册,没分下载.....不需要任何控件的,界面也不要,相当于放在初始化函数中的效果
shenyi0106 2014-10-15
  • 打赏
  • 举报
回复
100分好贵啊
微型蚂蚁 2014-10-15
  • 打赏
  • 举报
回复
网上随便搜索一下有很多。http://download.csdn.net/detail/xuyunhou/3166403

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧