aeroson

Untitled

May 5th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. //***************************************************************************
  2. //
  3. // Petr Olivka (c) 03/2013 pro projekt do predmetu OSY.
  4. // Viz. http://poli.cs.vsb.cz/edu/osy
  5. //
  6. // Ukazka kodu pro prijem a odesilani zprav.
  7. //
  8. // Prijem a odesilani zprav si bude kazdy ve svem projektu implementovat sam!
  9. //
  10. //***************************************************************************
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <stdarg.h>
  17. #include <time.h>
  18. #include <sys/socket.h>
  19. #include <sys/param.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <netdb.h>
  27.  
  28. #include "msg_io.h"
  29.  
  30. int send_proto_msg( int handle, char type, int NN, const char *text ) {
  31. char buffer[256];
  32. int len=sprintf (buffer, "%c%02d:%s\n", type, NN, text);
  33. m_print( M_DEBUG, "send %d %s",len,buffer);
  34. int ret=write(handle, buffer, len);
  35. return ret;
  36. }
  37.  
  38. int recv_proto_msg( int handle, char *type, int *NN, char *text ) {
  39. char buffer[256];
  40. int ret=read(handle,buffer,256);
  41. buffer[ret]='\0';
  42. m_print( M_DEBUG,"recv1 %d %s",ret,buffer);
  43. if(ret<=0) return ret;
  44.  
  45. *type=buffer[0];
  46. buffer[0]='0';
  47. char * pch;
  48. pch = strtok (buffer,":");
  49. *NN=atoi(pch);
  50. pch = strtok (NULL,":");
  51. strcpy(text,pch);
  52.  
  53. sprintf(buffer, "%c%02d:%s\n", *type, *NN, text);
  54. m_print( M_DEBUG,"recv2 %s",buffer);
  55. return ret;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment