socket编程(一)

基本的socket编程。

server:

#include <cstdlib>
#include <fstream>
#include <iomanip>   
#include <iostream>  

#include <string.h>
#include <errno.h> 
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>


using namespace std;



int main(int argc, char *argv[])
{
        int socket_desc, client_sock, c, read_size;
        struct sockaddr_in server, client;
        char client_message[2000];

        // create a socket
        socket_desc = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_desc == -1)
                perror("couldn't create socket");
        cout << "socket created" << endl;

        // prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(8888);

        // enable address reuse
        int on = 1;
        if (setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
                perror("setsockopt failed");
                return 1;
        }

        // bind
        if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
                perror("bind failed");
                return 1;
        }
        cout << "bind done" << endl;

        // listen
        listen(socket_desc, 3);
        
        // accept incoming connection ...
        cout << "Waitting for incoming connections ..." << endl;
        c = sizeof(struct sockaddr_in);
        client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
        if (client_sock < 0) {
                perror("accept failed");
                return 1;
        }
        cout << "connection accepted" << endl;

        // receive a message from client
        while(read_size = recv(client_sock, client_message, 2000, 0) > 0) {
                cout << client_message << endl;
                send(client_sock, client_message, strlen(client_message), 0);
        }
        
        if (read_size == 0) {
                cout << "client disconnected" << endl;
                fflush(stdout);
        } else if (read_size == -1) {
                perror("recv failed");
        }
        

        return 0;
}

client:

#include <cstdlib>
#include <fstream>
#include <iomanip>   
#include <iostream>  

#include <string.h>
#include <errno.h> 
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include<arpa/inet.h> //inet_addr


using namespace std;



int main(int argc, char *argv[])
{
        int sock;
        struct sockaddr_in server;
        char message[1000], server_reply[2000];

        // create socket
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock == -1)
                cout << "couldn't create socket" << endl;
        cout << "socket created" << endl;

        // prepare sockaddr_in server
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        server.sin_family = AF_INET;
        server.sin_port = htons(8888);

        // connect to remote server
        if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
                perror("connect failed");
                return 1;
        }
        cout << "Connected" << endl;

        // keep communicating with server
        while(1) {
                cout << "Enter message: " << endl;
                cin.getline(message, sizeof(message));

                // send some data
                if (send(sock, message, strlen(message), 0) < 0) {
                        cout << "Send failed" << endl;
                        return 1;
                }

                // receive a reply from the server
                if (recv(sock, server_reply, 2000, 0) < 0) {
                        cout << "recv failed" << endl;
                        break;
                }
                cout << "Server reply: " << server_reply << endl;
        }


	return 0;
}

参考:http://www.binarytides.com/server-client-example-c-sockets-linux/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值