Socket 在哪邊:
TCP:
UDP:
hostaddress 由4 byte的數字組成,socket中的struct in_addr 將這個整束包裝起來,定義在in.h
struct in_addr {
__be32 s_addr;
}
特殊的host address number:
l INADDR_LOOPBACK:指本機的address,也就是127.0,0,1(localhost)
l INADDR_ANY:指任何連上來的address
l INADDR_BROADCAST:傳送broadcast訊息才可使用
l INADDR_NONE:某些function錯誤時的回傳值
字節順序轉換函數:
htonl():把32位值從主機字節序轉換成網絡字節序
htons():把16位值從主機字節序轉換成網絡字節序
ntohl():把32位值從網絡字節序轉換成主機字節序
ntohs():把16位值從網絡字節序轉換成主機字節序
Bind函數將socket與本機上的一個埠相關聯.你就可以在該埠監聽服務請求
Client端:
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int sockfd=0,n=0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc!=2)
{
printf("\n Usage:%s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff,'0',sizeof(recvBuff));
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Error: Could not create socket \n");
return 1;
}
memset(&serv_addr,'0',sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(5000);
if(inet_pton(AF_INET,argv[1],&serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n Error:Connect Failed \n");
return 1;
}
while((n=read(sockfd,recvBuff,sizeof(recvBuff)-1))>0)
{
recvBuff[n]=0;
if(fputs(recvBuff,stdout)==EOF)
{
printf("\n Error:Fputs error \n");
}
}
if(n<0)
{
printf("\n Read error\n");
}
return 0;
}
SERVER端:
#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int listenfd=0,connfd=0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd=socket(AF_INET,SOCK_STREAM,0);
memset(&serv_addr,'0',sizeof(serv_addr));
memset(sendBuff,'0',sizeof(sendBuff));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(5000);
bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(listenfd,10);
while(1)
{
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
ticks=time(NULL);
snprintf(sendBuff,sizeof(sendBuff),"%.24s\r\n",ctime(&ticks));
write(connfd,sendBuff,strlen(sendBuff));
close(connfd);
sleep(1);
}
return 0;
}
#include <cstdlib>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int sockfd=0,n=0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc!=2)
{
printf("\n Usage:%s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff,'0',sizeof(recvBuff));
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Error: Could not create socket \n");
return 1;
}
memset(&serv_addr,'0',sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(5000);
if(inet_pton(AF_INET,argv[1],&serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n Error:Connect Failed \n");
return 1;
}
while((n=read(sockfd,recvBuff,sizeof(recvBuff)-1))>0)
{
recvBuff[n]=0;
if(fputs(recvBuff,stdout)==EOF)
{
printf("\n Error:Fputs error \n");
}
}
if(n<0)
{
printf("\n Read error\n");
}
return 0;
}
SERVER端:
#include <cstdlib>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int listenfd=0,connfd=0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd=socket(AF_INET,SOCK_STREAM,0);
memset(&serv_addr,'0',sizeof(serv_addr));
memset(sendBuff,'0',sizeof(sendBuff));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(5000);
bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(listenfd,10);
while(1)
{
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
ticks=time(NULL);
snprintf(sendBuff,sizeof(sendBuff),"%.24s\r\n",ctime(&ticks));
write(connfd,sendBuff,strlen(sendBuff));
close(connfd);
sleep(1);
}
return 0;
}
參考:
http://www.thegeekstuff.com/2011/12/c-socket-programming/
沒有留言:
張貼留言