如果您对socket通信不太了解,请先阅读http://blog.csdn.net/weixin_37977062/article/details/63878744
/* $Id: socket.c 1.1 1995/01/01 07:11:14 cthuang Exp $
*
* This module has been modified by Radim Kolar for OS/2 emx
*/
/***********************************************************************
module: socket.c
program: popclient
SCCS ID: @(#)socket.c 1.5 4/1/94
programmer: Virginia Tech Computing Center
compiler: DEC RISC C compiler (Ultrix 4.1)
environment: DEC Ultrix 4.3
description: UNIX sockets code.
***********************************************************************/
/*************************************************************************************************************
/*
* struct hostent {
* char *h_name; // 表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com
*
* char **h_aliases; // 表示的是主机的别名.www.google.com就是google他自己的别名。
* // 有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆
* // 而为自己的网站多取的名字
*
* int h_addrtype; // 表示的是主机ip地址的类型,到底是Ipv4(AF_INET),还是Ipv6(AF_INET6)
* int h_length; // 表示的是主机ip地址的长度
*
* char **h_addr_list; // 表示的是主机的ip地址,注意,这个是以网络字节序存储的
* // 千万不要直接用printf带%s参数来打这个东西,会有问题的哇。
* // 所以到真正需要打印出这个IP的话,需要调用inet_ntop()。
* #define h_addr h_addr_list[0]
* };
*/
*************************************************************************************************************
*****************************************
* TCP协议通信方式:
* 主动端 被动端
* 客户端 服务器端
* 建立一个套接字 建立一个套接字
* socket() socket()
* 绑定地址 绑定地址
* bind() bind()
* 连接 监听
* connect() listen()
* 接受
* accept()
* 数据通信《--------》数据通信
* 发送/接收
* send/recv
* 关闭 关闭
* close() close()
******************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/***********
功能:通过地址和端口建立网络连接
@host:网络地址
@clientPort:端口
Return:建立的socket连接。
如果返回-1,表示建立连接失败
************/
int Socket(const char *host, int clientPort)
{
int sock;
unsigned long inaddr;
struct sockaddr_in ad; //IPv4地址结构
struct hostent *hp;
//memset函数原型:void *memset(void *s, int c, size_t n);
//函数描述:The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
memset(&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;
inaddr = inet_addr(host);//将点分的十进制的IP转为无符号长整形
//确定IP
if (inaddr != INADDR_NONE) //INADDR_NONE 是个宏定义,代表IpAddress 无效的IP地址
//memcpy函数原型:void *memcpy(void *dest, const void *src, size_t n);
//The memcpy() function copies n bytes from memory area src to memory area dest.
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
else//如果host是域名
{
//gethostbyname函数原型:struct hostent *gethostbyname(const char *name);
//这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。
hp = gethostbyname(host);//用域名获取IP
if (hp == NULL)
return -1;
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
}
//端口号
//htons是将整型变量从主机字节顺序转变成网络字节顺序
ad.sin_port = htons(clientPort);
//创建套接字
//socket函数原型:int socket(int domain, int type, int protocol);
//函数描述:On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately.
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
return sock;
//连接
//connect函数原型:int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
//函数描述:If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
return -1;
return sock;
}
更多精彩内容,请点击:http://blog.csdn.net/weixin_37977062?viewmode=contents