經常用tcp套介面通訊,udp套介面也是很重要。今天使用了一下,小節如下:
無論是 server 還是 client,首先包含標頭檔案和庫
#include
#pragma comment(lib, "wsock32")
對於建立server:
1. wsastartup() 載入套接字型檔
2. socket() 建立套接字
3. bind() 繫結
4. recvfrom() 接收 / sendto() 傳送
5. closesocket() 關閉套接字
6. wsacleanup() 解除安裝套接字型檔
對於建立client: (不需要bind)
1. wsastartup() 載入套接字型檔
2. socket() 建立套接字
3. recvfrom() 接收 / sendto() 傳送
4. closesocket() 關閉套接字
5. wsacleanup() 解除安裝套接字型檔
server端**如下:(功能 - 接收從client端傳送的計算機使用資訊)
#include
#include
#pragma comment(lib, "wsock32")
typedef struct
computer_inform;
int main()
/* create socket */
socket sktsrv;
sktsrv = socket(af_inet, sock_dgram, 0 );
if( sktsrv == invalid_socket )
/* create address */
sockaddr_in addrsrv;
addrsrv.sin_family = af_inet;
addrsrv.sin_addr.s_un.s_addr = htonl(inaddr_any);
addrsrv.sin_port = htons(4345);
/* bind socket */
int result = bind(sktsrv,(sockaddr*)&addrsrv,sizeof(sockaddr));
if(result == socket_error)
/* receive data */
computer_inform info;
sockaddr addrclt;
int addrlen, recvlen;
uint index;
recvlen = -1;
index = 0;
addrlen = sizeof(sockaddr);
printf("begin to receive/n");
while(1)
/* close socket */
closesocket(sktsrv);
wsacleanup();
return 0;}
#include
#include
#pragma comment(lib, "wsock32")
#define addr_socket_srv "192.168.0.117"
#define server_update_interval 60000 // ms
#define client_update_interval 6000 // ms
#define systembasicinformation 0
#define systemperformanceinformation 2
#define systemtimeinformation 3
#define li2double(x) ((double)((x).highpart) * 4.294967296e9 + (double)((x).lowpart))
typedef
struct
computer_inform;
typedef
struct
system_performance_information;
typedef
struct
system_basic_information;
typedef
struct
system_time_information;
typedef
long (winapi *procntqsi)(uint,pvoid,ulong,pulong);
void main(void)
;large_integer lioldsystemtime = ;
computer_inform computerinfo;
socket m_sockclt=0;
char computername[30];
char username[30];
dword size=30;
wsadata wsadata;
sockaddr_in clientservice;
int ret;
int iresult;
procntqsi ntquerysysteminformation;
iresult = wsastartup(makeword(2,2), &wsadata);
if (iresult != no_error)
memset(&computerinfo,0,sizeof(computer_inform));
/* init the client socket */
m_sockclt = socket(af_inet, sock_dgram, 0);
if(m_sockclt == invalid_socket)
/* connect to server */
clientservice.sin_family = af_inet;
clientservice.sin_addr.s_addr = inet_addr(addr_socket_srv); //inet_addr("192.168.0.117" );
while(1)
else
}while(1)
else
}/* get computer name */
if(!getcomputername(computername,&size))
else
printf("/ncpu usage (press any key to exit): /n");
uint index = 0;
while(1)
//printf("/b/b/b/b%3d%%/n",(uint)dbidletime);
//printf("index = %d/t", index);
/* get user name */
if(!getusername(username,&size))
else
/* write the cpu usage rate to the struct */
computerinfo.cpurate = (uint)dbidletime;
//printf("%4.1d%%/n",(uint)dbidletime);
//ret = send(m_csocket,(char *)&computerinfo,sizeof(computerinfo),0);
printf("%d, %s: %s, %3.1d%%/n",
index, computerinfo.computername, computerinfo.username, computerinfo.cpurate);
ret = sendto(m_sockclt, (char *)&computerinfo, sizeof(computerinfo), 0,
(sockaddr *)&clientservice, sizeof(sockaddr));
if(ret == socket_error)
}index ++;
/* store new cpu's idle and system time */
lioldidletime = sysperfinfo.liidletime;
lioldsystemtime = systimeinfo.likesystemtime;
/* wait one minute */
//sleep(6000);
sleep(client_update_interval);
}printf("/n");
}
套介面程式設計
1 struct in addr 4struct sockaddr in 通用套介面位址結構 struct sockaddr int bind int struct sockaddr socklen t strcut sockaddr in serv bind sockfd,struct socka...
套介面程式設計
1 struct in addr 4struct sockaddr in struct sockaddr int bind int struct sockaddr socklen t strcut sockaddr in serv bind sockfd,struct sockaddr serv,s...
套介面程式設計
1 struct in addr 4struct sockaddr in struct sockaddr int bind int struct sockaddr socklen t strcut sockaddr in serv bind sockfd,struct sockaddr serv,s...