套接字编程_#define

套接字编程_套接字_02

一,创建套接字

int socket(int domain,int type,int protocol)

socket 返回一个套接字描述符

StreamSocket = socket(AF_INET,SOCK_STREAM,0)  TCP类型

DgramSocket = socket(AF_INET,SOCK_DGRAM,0) UDP类型

RawSocket = sokcet(AF_INET,SOCK_RAW,IPPROTO_RAW)

AF_INET表示要使用IPv4 Internet协议,第二个参数是协议的通信类型,TCP,UDP等

二,套接字地址

struct sockaddr_in{
int16_t sin_family;
uint16_t sin_port; 端口号
struct in_addr sin_addr; IPv4的地址
char sin_zero[8];
}
struct in_addr{
uint32_t s_addr;
}

为了进行通信 用AF_INET唯一确定 sin_family.

三,套接字原语

int bind(int sock,struct sockaddr *addr,int addrLen)

用于绑定套接字和地址

sock 套接字描述符

地址结构体已经确定,所以必须使用(sockaddr*)将sockaddr_in 转化为sockaddr类型

int listen(int sock,int backlog)

backlog 指明等待连接的客户端队列中可以有几个客户端

int accept(int sock,struct sockaddr *addr,int *addrLen)

addr 是想知道谁向自己发送连接请求

struct sockaddr_in addr;

int len;

len = sizeof(struct sockaddr_in);

sock = accept(serversock,(struct sockaddr*)addr,&len)

如果对谁的请求不关心

accept(serversock,(struct sockaddr*,NULL,NULL)

int connect(int sock,(struct sockaddr*)servaddr,int addLen)

四,套接字输入输出

TCP 面向连接的

int send(int sock,const void*msg,int len,unsigned int flags)

返回<0代表错误类型,>0代表传递的字节数。

msg 消息,len 消息长度,flag 基本为0 可以设定send的行为如MSG_DONTWAIT 当可用空间不足时send 就返回

int recv(int sock,void*buf,int len,unsigned int flags);

UDP

int sendto(int sock,const void*msg,int len,unsigned int flags,const struct sockaddr *to,int tolen)

int recvform(int sock,void*buf,int len,unisgned int flags,sturct sockaddr *from,int *fromlen)

五,套接字选项

int getsockopt(int sock,int level,int optname,void *optval,socklen_t *optlen);

int setsockopt(int sock,int level,int optname,const void *optval,socklen_t optlen)


struct hostnet *gethostbyname(const char*name)

eg:

struct hostnet*hptr;

hptr = gethostbyname("www.baidu.com")其实就是域名解析

int getsockname(int sock,struct sockaddr *name,socklen_t*namelen)

int getpeername(int sock,struct sockaddr*name,socklen_t *namelen)

请求时间获取服务器


/*************************************************************************
> File Name: server_of_date.c
> Author: nealgavin

> Created Time: Tue 03 Jun 2014 08:02:38 PM CST
************************************************************************/

#include<stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 1413

int main()
{
int serverSK,connectSK;
struct sockaddr_in servaddr;
char timebuffer[ MAX_BUFFER ];
time_t currentTime;

serverSK = socket(AF_INET,SOCK_STREAM,0);
/* AF_INET confirm use IPv4 Protocol
* SOCK_STREAM confirm Stream type
* 0 confirm TCP
*/

memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*receive any type of socket*/
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);

bind( serverSK,(struct sockaddr *)&servaddr,sizeof(servaddr) );
/*bing the socket and address*/

listen(serverSK,5);
puts("start tel");
while(1)
{
connectSK = accept( serverSK,(struct sockaddr*)NULL,NULL );
if (connectSK >= 0)
{
printf("client connet to server.Socket:%d\n",connectSK);
currentTime = time(NULL);
snprintf(timebuffer, MAX_BUFFER,"%s\n",ctime(¤tTime));
write( connectSK,timebuffer,strlen(timebuffer) );
close( connectSK );
}
}
return 0;
}



/*************************************************************************
> File Name: client_of_date.c
> Author: nealgavin

> Created Time: Tue 03 Jun 2014 08:36:18 PM CST
************************************************************************/

#include<stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 1413

int main()
{
int connectSK,in,index=0,limit=MAX_BUFFER,ret;
struct sockaddr_in servaddr;
char timeBuffer[ MAX_BUFFER+1 ];

connectSK = socket(AF_INET,SOCK_STREAM,0);

memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DAYTIME_SERVER_PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

ret = connect(connectSK,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("client connect to server.ans=%d\n",ret);
if(ret == -1)
{
puts("connect false");
return 0;
}
while((in = read(connectSK,&timeBuffer[index],limit))>0)
{
index += in;
limit -= in;
}
timeBuffer[index] = '\0';
printf("\ndate:%s\n",timeBuffer);
close(connectSK);
return 0;
}