[tcp/ip] 有链接交互式通信

client.cpp

#include <Winsock2.h>
#include <stdio.h>
#define DEFAULT_PORT 5050
#define DATA_BUFFER 1024
int main(int argc,char *argv[])
{
    WSADATA wsaData;
    SOCKET sClient;
    int iPort = DEFAULT_PORT;
    int iLen,iSend;
    char buf[DATA_BUFFER];
    struct sockaddr_in ser;
    if(argc<2)
    {
        printf("Usage:client [server IP address]\n");
        return 0;
    }
    memset(buf,0,sizeof(buf));
    if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    {
        printf("Failed to load Winsock.\n");
        return 0;
    }
    ser.sin_family = AF_INET;
    ser.sin_port = htons(iPort);
    ser.sin_addr.s_addr=inet_addr(argv[1]);
    sClient = socket(AF_INET,SOCK_STREAM,0);
    if(sClient==INVALID_SOCKET)
    {
    	 printf("socket()Failed:%d\n",WSAGetLastError());
        return 0;
    }
    if(connect(sClient,(struct sockaddr*)&ser,sizeof(ser))==INVALID_SOCKET)
    {
    	 printf("connect()Failed:%d\n",WSAGetLastError());
        return 0;
    }
    else
    {
    	/*while(1)
        {
            iLen=recv(sClient,buf,sizeof(buf),0);
    	if(iLen==0)
    	{
    		return 0;
    	}
    	else if(iLen==SOCKET_ERROR)
    	{
    		printf("recv()Failed:%d\n",WSAGetLastError());
            return 0;
    	}
    	if(buf[0]=='#') printf("break");
    	printf(":%s\n",buf);

        }*/
        while(1)
       {
        gets(buf);
        iSend = send(sClient,buf,sizeof(buf),0);
        if(iSend==SOCKET_ERROR)
        {
            printf("send()Failed:%d\n",WSAGetLastError());
            break;
        }
        else if(iSend==0) break;
        else
        {
           // printf("send()byte:%d\n",iSend);
           // printf("-----------------------\n");
        }
        if(buf[0]=='#')break;
    }
    }
    closesocket(sClient);
    WSACleanup();

    return 0;
}

server.cpp

#include<Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5050
int main()
{
    while(1)
    {
    int iPort = DEFAULT_PORT;
    WSADATA  wsaData;
    SOCKET   sListen,sAccept;
    int iLen;
    int iSend;
    char buf[100000];
    struct sockaddr_in ser,cli;
    if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    {
        printf("Failed to load Winsock.\n");
        return 0;
    }
    sListen = socket(AF_INET,SOCK_STREAM,0);
    if(sListen == INVALID_SOCKET)
    {
        printf("socket()Failed:%d\n",WSAGetLastError());
        return 0;
    }
    ser.sin_family = AF_INET;
    ser.sin_port = htons(iPort);
    ser.sin_addr.s_addr=htonl(INADDR_ANY);
    if(bind(sListen,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR)
    {
        printf("bind()Failed:%d\n",WSAGetLastError());
        return 0;
    }
    if(listen(sListen,5)==SOCKET_ERROR)
    {
        printf("listen()Failed:%d\n",WSAGetLastError());
        return 0;
    }
    iLen = sizeof(cli);
    sAccept = accept(sListen,(struct sockaddr*)&cli,&iLen);
        if(sAccept==INVALID_SOCKET)
        {
            printf("accept()Failed:%d\n",WSAGetLastError());
            return 0;
        }
   // printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
       /*while(1)
       {
           scanf("%s",buf);
        iSend = send(sAccept,buf,sizeof(buf),0);
        if(iSend==SOCKET_ERROR)
        {
            printf("send()Failed:%d\n",WSAGetLastError());
            break;
        }
        else if(iSend==0) break;
        else
        {
           // printf("send()byte:%d\n",iSend);
           // printf("-----------------------\n");
        }
        if(buf[0]=='#')break;
    }*/
        while(1)
        {
           // cout<<"ee"<<endl;
            iLen=recv(sAccept,buf,sizeof(buf),0);
    	if(iLen==0)
    	{
    		break;
    	}
    	else if(iLen==SOCKET_ERROR)
    	{
    		printf("recv()Failed:%d\n",WSAGetLastError());
           break;
    	}
    	if(buf[0]=='#') printf("quit\n");
    	else {printf("Eric:");puts(buf);}
    	//cout<<"tt"<<endl;

        }
    closesocket(sAccept);
    closesocket(sListen);
    WSACleanup();
    }
    return 0;
}

全部评论

相关推荐

头像
11-27 14:28
长沙理工大学
刷算法真的是提升代码能力最快的方法吗?&nbsp;刷算法真的是提升代码能力最快的方法吗?
牛牛不会牛泪:看你想提升什么,代码能力太宽泛了,是想提升算法能力还是工程能力? 工程能力做项目找实习,算法也分数据结构算法题和深度学习之类算法
点赞 评论 收藏
分享
感性的干饭人在线蹲牛友:🐮 应该是在嘉定这边叭,禾赛大楼挺好看的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
11-24 11:23
门头沟学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务