TCP/IP socket programming in C(一)

// client.c
#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/socket.h>
#include <netinet/in.h> /* inet_addr */


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

        // Create socket
        socket_desc = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_desc == -1) {
                printf("Could not create socket\n");
        }

        server.sin_addr.s_addr = inet_addr("74.125.235.20");
        server.sin_family = AF_INET;
        server.sin_port = htons(80);

        // Connect to remote server
        if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
                puts("connect error\n");
                return 1;
        }

        puts("Connected\n");

        // Send some data
        message = "GET / HTTP/1.1\r\n\r\n";
        if (send(socket_desc, message, strlen(message), 0) < 0) {
                puts("Send failed\n");
                return 1;
        }
        puts("Data Send\n");

        // Receive a reply from the server
        if (recv(socket_desc, server_reply, 2000, 0) < 0) {
                puts("recv failed");
        }
        puts("Reply received\n");
        puts(server_reply);

        // Close socket
        close(socket_desc);
        
	return 0;
}


参考:http://www.binarytides.com/socket-programming-c-linux-tutorial/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TCP/IP Sockets In C 第二版 英文版 Changes from the First Edition We have updated and considerably expanded most of the material, having added two chapters. Major changes from the first edition include:  IP version 6 coverage. We now include three kinds of code: IPv4-specific, IPv6-specific, and generic. The code in the later chapters is designed to work with either protocol version on dual-stack machines.  An additional chapter on socket programming in C++ (contributed by David B. Sturgill). The PracticalSocket library provides wrappers for basic socket functionality. These allow an instructor to teach socket programming to students without C programming back- ground by giving them a library and then gradually peeling back the layers. Students can start developing immediately after understanding addresses/ports and client/server. Later they can be shown the details of socket programming by peeking inside the wrapper code. Those teaching a subject that uses networking (e.g., OS) can use the library and only selectively peel back the cover.  Enhanced coverage of data representation issues and strategies for organizing code that sends and receives messages. In our instructional experience, we find that students have less and less understanding of how data is actually stored in memory, 1 so we have attempted to compensate with more discussion of this important issue. At the same time, internationalization will only increase in importance, and thus we have included basic coverage of wide characters and encodings.  Omission of the reference section. The descriptions of most of the functions that make up the Sockets API have been collected into the early chapters. However, with so many online sources of reference information—including “man pages”—available, we chose to leave out the complete listing of the API in favor of more code illustrations.  Highlighting important but subtle facts and caveats. Typographical devices call out important concepts and information that might otherwise be missed on first reading. Although the scope of the book has expanded, we have not included everything that we might have (or even that we were asked to include); examples of topics left for more comprehensive texts (or the next edition) are raw sockets and programming with WinSock.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值