728x90
반응형
오전 수업내용
tcp.h
#ifndef __TCP_H__ #define __TCP_H__ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> #include <sys/stat.h> #include <sys/signal.h> #include <stdlib.h> #include <sys/time.h> #define BSIZE 250 #define IP_SERVER "192.168.1.60" #define PORT 10000 #define MAXUSER 3 #endif //__TCP_H__
server.c
#include "tcp.h" int iCSock[MAXUSER]; int iSSock; int iUser; /* void sRead(int iNum) { char caBuff[BSIZE]; int iRet; iRet=read(iCSock , caBuff, BSIZE); caBuff[iRet-1]=0; printf("\n Incomming Message: [%s]\n", caBuff); printf("input Message: "); fflush(stdout); } */ void End(int iNum) { int iCnt; close(iSSock); for(iCnt=0; iCnt<iUser; ++iCnt) { write(iCSock[iCnt],"Server is closed", strlen("Server is closed")); close(iCSock[iCnt]); printf("Die [%d]persons \n", iCnt+1); } exit (0); } int main(int iArg, char *cpAcmd[]) { int iRet; int iLen; int iTSock; int iCnt; int iCnt2; char caBuff[BSIZE]; int iMaxFD; fd_set stRFd; short sPort; struct sockaddr_in stServer; struct sockaddr_in stClient; if(iArg==1) { sPort=PORT; } else { sPort=atoi(cpAcmd[1]); } iSSock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(iSSock<0) { printf("can not make a socket \n"); perror("iSSock"); return 0; } iLen= sizeof(stClient); bzero(&stServer, iLen); stServer.sin_family = AF_INET; stServer.sin_addr.s_addr = INADDR_ANY; //stServer.sin_port = htons(PORT); stServer.sin_port = htons(sPort); iRet=bind(iSSock, (struct sockaddr *)&stServer, iLen); if(iRet==-1) { printf("binding error \n"); perror("bind"); close(iSSock); return 0; } iRet=listen(iSSock, 5); if(iRet==-1) { printf("listen error \n"); perror("listen"); close(iSSock); return 0; } printf("server is running ...[%d] \n", sPort); signal(SIGINT, End ); iUser=0; iMaxFD=iSSock; while(1) { printf("input Message: "); fflush(stdout); FD_ZERO(&stRFd); FD_SET(0,&stRFd); for(iCnt=0;iCnt<iUser;++iCnt) { FD_SET(iCSock[iCnt], &stRFd); } FD_SET(iSSock, &stRFd); select(iMaxFD+1, &stRFd, NULL, NULL, NULL); if(FD_ISSET(iSSock, &stRFd)!=0) { iTSock=accept(iSSock, (struct sockaddr *)&stClient, &iLen); if(iTSock==-1) { printf("accept error \n"); perror("accept"); continue; } if(iUser>=MAXUSER) { close(iTSock); printf("Maxuser is over the number \n"); continue; } if(iMaxFD<iTSock) { iMaxFD=iTSock; } iCSock[iUser]=iTSock; ++iUser; } if(FD_ISSET(0, &stRFd)!=0) { iRet=read(0, caBuff, BSIZE); for(iCnt=0; iCnt<iUser; ++iCnt) { write(iCSock[iCnt], caBuff, iRet); } } for(iCnt=0; iCnt<iUser; ++iCnt) { if(FD_ISSET(iCSock[iCnt], &stRFd)!=0) { iRet=read(iCSock[iCnt], caBuff, BSIZE); if(iRet==0) { printf("client is disconnected [%d] \n",iCnt); if(iMaxFD==iCSock[iCnt]) { for(iCnt2=0,iMaxFD=iCSock[0];iCnt2<iUser;++iCnt2) //³»²¨´Â iCnt, ¾Æ¶÷¾¾´Â iUser { if(iCnt==iCnt2) { continue; } if(iMaxFD<iCSock[iCnt2]) { iMaxFD=iCSock[iCnt2]; } } } close(iCSock[iCnt]); //¼ÒÄÏÀ» ´Ý¾ÆÁÖ´Â ¿ªÇÒÀ» ÇÑ´Ù. --iUser; iCSock[iCnt]=iCSock[iUser]; //Á¦ÀÏ µÚ¿¡ ÀÖ´Â À¯Àú¸¦ ³ª°£ À¯ÀúÀÇ ÀÚ¸®¿¡ ¹èÄ¡ÇÑ´Ù. continue; } caBuff[iRet-1]=0; for(iCnt2=0; iCnt2<iUser; ++iCnt2) { if(iCnt==iCnt2) { continue; } write(iCSock[iCnt2], caBuff, iRet); //¸ðµç »ç¶÷¿¡°Ô ¸Þ½ÃÁö¸¦ º¸³½´Ù.(³ª¸¦ Æ÷ÇÔ) } printf("\n [client: %s] \n", caBuff); } } } return 0; }
client.c
#include "tcp.h" int iCSock; void End(int iNum) { close(iCSock); exit(0); } void sRead(int iNum) { char caBuff[BSIZE]; int iRet; iRet=read(iCSock , caBuff, BSIZE); caBuff[iRet-1]=0; printf("\n Incomming Message: [%s]\n", caBuff); printf("input Message: "); fflush(stdout); } int main(int iArg, char *cpAcmd[]) { int iRet; int iLen; fd_set stRFd; char caBuff[BSIZE]; short sPort; struct sockaddr_in stServer; if(iArg==1) { sPort=PORT; } else { sPort=atoi(cpAcmd[1]); } iCSock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(iCSock<0) { printf("can not make a socket \n"); perror("iCSock"); return 0; } iLen=sizeof(stServer); bzero(&stServer, iLen); stServer.sin_family = AF_INET; stServer.sin_addr.s_addr =inet_addr(IP_SERVER); //stServer.sin_port = htons(PORT); stServer.sin_port = htons(PORT); iRet=connect(iCSock, (struct sockaddr *)&stServer, iLen); if(iRet==-1) { printf("connect error \n"); perror("connect"); close(iRet); return 0; } printf("server is running ... \n"); signal(SIGINT, End); while(1) { printf("input Message: "); fflush(stdout); FD_ZERO(&stRFd); FD_SET(0,&stRFd);//Å°º¸µå¿¡¼ ¹ÞÀº °ªÀ» ±¸Á¶Ã¼¿¡ ÀúÀåÇÑ´Ù. FD_SET(iCSock, &stRFd);//³×Æ®¿öÅ©¿¡¼ ¹ÞÀº °ªÀ» ÀúÀåÇÑ´Ù. select(iCSock+1, &stRFd, NULL, NULL, NULL); if(FD_ISSET(0, &stRFd)!=0) { iRet=read(0, caBuff, BSIZE); write(iCSock, caBuff, iRet); } if(FD_ISSET(iCSock, &stRFd)!=0) { iRet=read(iCSock, caBuff, BSIZE); if(iRet==0) { printf("server is disconnected \n"); End(0); return 0; } caBuff[iRet-1]=0; printf("\n [server: %s] \n", caBuff); } } close(iCSock); return 0; }
<출력결과>
오후 수업내용(패킷을 볼 수 있는 것에 대해서 공부를 한다. 도덕심을 키워서 좋은 쪽으로만 사용하도록 하자.)
Network Protocol Analyzer
<참고 자료 주소>
http://www.joinc.co.kr/w/Site/Network_Programing/AdvancedComm/pcap_intro
sudo apt-get install libpcap-dev
ll pcap *
cd /usr/include/net -> vi ethernet.h
소스
#include <stdio.h> #include <pcap.h> #include <pcap/pcap.h> int main(void) { char * cpNicname; //Network interface card name char errbuf[PCAP_ERRBUF_SIZE]; //man pcap_lookupdev에서 가지고 온다. char *pcap_lookupdev(char *errbuf); cpNicname=pcap_lookupdev(errbuf); printf("Nicname: [%s] \n", cpNicname); return 0; }
<출력결과>
gcc -o main main.c -lpcap
헥사뷰로 보기
<소스>
#include <stdio.h> #include <pcap.h> #include <errno.h> void Hexaview(const unsigned char *ucpData, unsigned int uiLen) { unsigned int uiCnt; unsigned int ui16Cnt; printf("===================================== \n"); printf("============ Hexa Viewer ============ \n"); uiCnt = 0; while(160 > uiCnt) { printf("%p ", uiCnt); ui16Cnt = 0; while(16 > ui16Cnt) { printf("%02X ", *ucpData); ui16Cnt = ui16Cnt + 1; ucpData = ucpData + 1; } printf("\n"); printf("%p \n", uiCnt); uiCnt = uiCnt + 16; } } int main(void) { char * cpNicname; //Network interface card name char errbuf[PCAP_ERRBUF_SIZE]; //man pcap_lookupdev에서 가지고 온다. char *pcap_lookupdev(char *errbuf); const unsigned char * ucpData; struct pcap_pkthdr stpInfo; pcap_t * stpHandle; cpNicname=pcap_lookupdev(errbuf); if(0==cpNicname) { perror(errbuf); return 0; } printf("Nicname: [%s] \n", cpNicname); stpHandle=pcap_open_live(cpNicname, 1500, 1, 0, errbuf); //pcap_t stphandle_open_live(cpNicname, 1500(ethernet), 1(1을해야 promis if(stpHandle==0) { perror(errbuf); return 0; } ucpData=pcap_next(stpHandle, &stpInfo); Hexaview(ucpData, 0); pcap_close(stpHandle); return 0; }
<출력결과>
<소스>
#include <stdio.h> #include <pcap.h> #include <errno.h> #include <net/ethernet.h> void Hexaview(const unsigned char *ucpData, unsigned int uiLen) { unsigned int uiCnt; unsigned int ui16Cnt; printf("===================================== \n"); printf("============ Hexa Viewer ============ \n"); uiCnt = 0; while(160 > uiCnt) { printf("%p ", uiCnt); ui16Cnt = 0; while(16 > ui16Cnt) { printf("%02X ", *ucpData); ui16Cnt = ui16Cnt + 1; ucpData = ucpData + 1; } printf("\n"); printf("%p \n", uiCnt); uiCnt = uiCnt + 16; } } int main(void) { char * cpNicname; //Network interface card name char errbuf[PCAP_ERRBUF_SIZE]; //man pcap_lookupdev¿¡¼ °¡Áö°í ¿Â´Ù. char *pcap_lookupdev(char *errbuf); const unsigned char * ucpData; const struct ether_header * stEH; struct pcap_pkthdr stpInfo; pcap_t * stpHandle; cpNicname=pcap_lookupdev(errbuf); if(0==cpNicname) { perror(errbuf); return 0; } printf("Nicname: [%s] \n", cpNicname); stpHandle=pcap_open_live(cpNicname, 1500, 1, 0, errbuf); //pcap_t stphandle_open_live(cpNicname, 1500(ethernet), 1(1À»ÇØ¾ß promisc°¡ ÄÑÁø´Ù.ÀÌ°ÍÀú°Í ´Ù ijġ), , ); if(stpHandle==0) { perror(errbuf); return 0; } ucpData=pcap_next(stpHandle, &stpInfo); Hexaview(ucpData, 0); stEH=(struct ether_header *)ucpData; printf("=================================================== \n "); printf("ETHERNET HEADER Into \n "); printf("=================================================== \n "); printf("Dest MAC: %02X: %02X: %02X: %02X: %02X: %02X: \n", stEH->ether_dhost[0], stEH->ether_dhost[1], stEH->ether_dhost[2], stEH->ether_dhost[3], stEH->ether_dhost[4], stEH->ether_dhost[5]); printf("=================================================== \n "); printf("Src MAC: %02X: %02X: %02X: %02X: %02X: %02X: \n", stEH->ether_shost[0], stEH->ether_shost[1], stEH->ether_shost[2], stEH->ether_shost[3], stEH->ether_shost[4], stEH->ether_shost[5]); printf("=================================================== \n "); pcap_close(stpHandle); return 0; }
<출력결과>
728x90
'코스웨어 > 16년 스마트컨트롤러' 카테고리의 다른 글
20160426_김도관_업무일지_디지털제어_RPi GPIO 제어 (2) | 2016.04.27 |
---|---|
2016-04-26_조재찬_업무일지_디지털제어-RPi GPIO 제어 (0) | 2016.04.26 |
20160425_김도관_업무일지_디지털제어_회로 기초 (0) | 2016.04.26 |
2016-04-25_조재찬_업무일지_디지털제어-회로 기초 (0) | 2016.04.25 |
20160420_장진웅_업무일지_공장내Network_일대일통신5_멀티채팅 (1) | 2016.04.20 |
20160419_장진웅_업무일지_공장내Network_일대일통신4_양방향 채팅 (0) | 2016.04.20 |
20160418_장진웅_업무일지_공장내Network_일대일통신3 (0) | 2016.04.18 |
버츄얼박스 com 개체를 만들수 없습니다 오류 (1) | 2016.04.17 |