UDP

Education/Networking 2009. 8. 27. 18:09


<server_udp.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>


void error(char *msg)
{

 perror(msg);
 exit(0);

}

int main(int argc, char *argv[])
{

 int sock, length, fromlen, n;
 struct sockaddr_in server;
 struct sockaddr_in from;

 char buf[1024];

 if(argc < 2)
 {

  fprintf(stderr, "ERROR, no port provided\n");
  exit(0);

 }

 sock = socket(AF_INET, SOCK_DGRAM, 0);

 if(sock <0) error("Opening socket");

 length = sizeof(server);

 bzero(&server, length);
 server.sin_family = AF_INET;
 server.sin_addr.s_addr  = INADDR_ANY;
 //server.sin_port  = htons(atoi(argv[1]));
 server.sin_port  = htons(8024);
 
 if( bind(sock, (struct sockaddr *)&server, length) < 0 )
  error("Binding");
 fromlen = sizeof(struct sockaddr_in);

 while (1)
 {

  n = recvfrom(sock, buf, 256, 0, (struct sockaddr *)&from, &fromlen);

  if(n <0)
  { 

   error("recvfrom");
   printf("Receive size 0\n");
   exit(-1);

  }
  
  write( 1, "Received a datagram: ", 21);
  buf[n] = '\0';
  write( 1, buf, n);

  n = sendto(sock,"Got your msg\n",13, 0, (struct sockaddr *)&from, sizeof(from));

  if(n <0) error("sendto");

 }

}



<client_udp.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>

void error(char *);

int main(int argc, char *argv[])
{

 int sock, length, n;
 struct sockaddr_in server, from;
 struct hostent *hp;

 char buffer[256];
 
 if(argc != 3)
 {

  printf("Usage: server port \n");
  exit(1);

 }

 sock = (AF_INET, SOCK_DGRAM, 0);
 if(sock <0) error("Socket\n");

 server.sin_family = AF_INET;
 hp = gethostbyname(argv[1]);
 if(hp==0) error("Unknown host\n");

 bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
 //server.sin_port = htons(atoi(argv[2]));
 server.sin_port = htons(8024);
 length = sizeof(struct sockaddr_in);
 printf("Please enter the message");

 bzero(buffer, 256);
 fgets(buffer, 255, stdin);

 n = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr *)&server, sizeof(server));
 if(n < 0) error("Sendto\n");

 n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&server, &length);
 if(n < 0) error("recvfrom\n");

 buffer[n] = '\0';

 write(1, "Got an ack: ",12);
 write(1, buffer,n);

}

void error(char *msg)

 perror(msg);
 exit(0);

}

'Education > Networking' 카테고리의 다른 글

TCP  (1) 2009.08.27
Posted by 초상큼발랄
l

Module

Education/Device Driver 2009. 8. 26. 19:46

Major :  장치의 종류
장치의 Interface
어떤 장치 드라이버와 연관이 되어 있는지를 알려준다.
장치 드라이버를 찾아주는 역할
Minor:  실제 장치들에 대한 식별
실제 장치 구분


 
<hello_mod.c>

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

int hello_init(void)
{
printk("<0>Hello Module Loaded \n");
return 0;
}
void hello_exit(void)
{
printk("<0>Hello Module Unloaded\n");
return ;
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");



<Makefile>

obj := hello_mod.o

KDIR := /lib/modules/$(shell uname -r)/bild               // directory module compile -> 모듈 설치
lib/moudles/ 해당 커널 버젼 Directory
PWD := $(shell pwd)

all :
$(MAKE) -C (KIDR)SUBDIRS=$(PWD)modules
clean:
rm -f *.ko
rm -f *.mod.*
rm -f Mod.*
rm -f *.o


 
#make

# insmod hello.ko
# lsmod | grep hello
# rmmod hello_mod
#lsmod |grep hello
dmesg | tail -2



Module shell Commands

1) insmod (insert module) 

2) rmmod (removemodule)

3) lsmod  (list of module)

4) modprobe ( dependency of module)
- depmod에 위해서 생성
- module 적재와 관련된 명령어
5) depmod 
- module's dependency 를 구축하기 위해 사용하는 모듈

 


 
Posted by 초상큼발랄
l