设为首页 收藏本站
查看: 927|回复: 0

[经验分享] distributed programming---lab1(basic communication of server and client)

[复制链接]

尚未签到

发表于 2018-10-21 14:50:46 | 显示全部楼层 |阅读模式
socket() creates an endpoint for communication and returns a file  
       descriptor that refers to that endpoint.
  The lab is about basic communication of server and client.
  The server is iterative server which only serves one client at a time.
The socket has the indicated type, which specifies the communication  
       semantics.  2 common types are:       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-
  
                       based byte streams.  An out-of-band data transmission
  
                       mechanism may be supported.       SOCK_DGRAM      Supports datagrams (connectionless, unreliable
  
                       messages of a fixed maximum length).
  
       SOCK_DGRAM as the type gets you a UDP socket.  You can encapsulate
  
UDP in TCP of course, but you'd have to handle the UDP part in
  
userspace.  Whether something is reliable depends on the protocol on the
  
wire.  If you use TCP, it is reliable; UDP is not.
  
To create a UDP socket:
  
int s;
  
s = socket(AF_INET, SOCK_DGRAM, 0);
  
To create a TCP socket:
  
int s;
  
s = socket(AF_INET, SOCK_STREAM, 0);
  
There are two common ways to send discrete chunks of data across the
  
wire.  You can either use UDP and send a discrete chunk as a datagram,
  
or you can but structs into TCP data, and let them go as a stream.
  
Using TCP is generally simpler and less failure-prone.  If you use UDP,
  
just time out and keep requesting the same data until you get it.
  Steps:
  (1).1st terminal
  gcc  -o server_test server_test.c errlib.c sockwrap.c
  ./server_test -a 1500
  2nd
  telnet 127.0.0.1 1500
  result
  Trying 127.0.0.1...
  Connected to 127.0.0.1.
  Escape character is '^]'.
  2 7
  9
  close connection;

  CTRL +>  gcc  -g -o server_test server_test.c errlib.c sockwrap.c//needs to compile all c files used
  valgrined ./server_test 127.0.0.1 1500
  valgrindThe Valgrind tool suite provides a number of debugging and
profiling tools that help you make your programs faster and more correct.
The most popular of these tools is called Memcheck.  It can detect many
memory-related errors that are common in C and C++ programs and that can
lead to crashes and unpredictable behaviour.

  The rest of this guide gives the minimum information you need to start
detecting memory errors in your program with Memcheck.  For full
documentation of Memcheck and the other tools, please read the User Manual.
Preparing your program:
  Compile your program with -g to include debugging
information so that Memcheck's error messages include exact line
numbers.  Using -O0 is also a good
idea, if you can tolerate the slowdown.  With-O1 line numbers in error messages can
be inaccurate,>-O1 works fairly well, and the speed improvement
compared to running -O0 is quite significant.
Use of-O2 and above is not recommended as
Memcheck occasionally reports uninitialised-value errors which don't
really exist.http://valgrind.org/docs/manual/quick-start.html

  )
  no leakage:
  ==3112== All heap blocks were freed -- no leaks are possible
  Use the functions Socket() and Connect() after having parsed the port number and the address

  through the Getaddrinfo() function or, as>
  two>  information of Getaddrinfo():
  http://linux.die.net/man/3/getaddrinfo
  (2)(3)
  write client which needs to use another termial tap,and opens wireshark before running server.
  (4)
  use udp which for server,doesn't need to use listen(),accept(),close();for client,doesn't need to use connect().
  1.whether client needs to bind or not?
  On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use. Generally you don't care, but in some cases, there may be a firewall on the client that only allows outgoing connections on certain port. In that case, you will need to bind to a specific port before the connection attempt will work.
  2.select()
  used in 2 cases:
  (1)when have more than 1 file descriptors
  (2)when want to set timeout,not always wait,just till TIMEOUT

  select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some>  3.SCNu16
  If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t, I’d use SCNu16 from , like this:
  #include
  #include
  uint16_t x;
  char *xs = "17";
  sscanf(xs, "%" SCNu16, &x);
  sscanf(buf,"%hu %hu",&op1,&op2);
  printf( "%hu" ,  ‐1 );
  "%hu" is an unsigned short int which is 16 bit.
  -1 is "all-once", e.g. 0xffff.ffff, but since it gets converted to short it is only 0xffff. Which is 65535 as unsigned.
  4.address
  If it says 0.0.0.0 on the Local Address column, it means that port is listening on all 'network interfaces' (i.e. your computer, your modem(s) and your network card(s))
  why is loopback IP address from 127.0.0.1 to 127.255.255.254?
  127.0.0.1 is the loopback Internet protocol (IP) address also referred
to as the “localhost.” The address is used to establish an IP connectionto the same machine or computer being used by the end-user. The same
convention is defined for computer’s that support IPv6 addressing using
the connotation of ::1. Establishing a connection using the address 127.0.0.1is the most common practice; however, using any IP address in the rangeof 127.*.*.* will function in the same or similar manner. The loopback
construct gives a computer or device capable of networking the
capability to validate or establish the IP stack on the machine.

  The 127/8 network can be used for a number of things.
  1) Simulating a large number of different computers in a fast network(simply bring up more interfaces and bind services to them) without
using virtual machines.  This might be helpful if you wanted to have a
number of different web servers running locally on port 80 for some
reason.
  2) Permitting more locally running services than the 64k TCP would
permit (though it seems unlikely that you would hit that limit
rationally)
  3) Playing games with people who aren't familiar with this factoid;
"Hey, you're a loser hacker, I bet you can't even hack me.  Go ahead andtry; I'm at 127.45.209.66"
  Probably other things too.
  5.array and string
  http://www.thegeekstuff.com/2011/12/c-arrays/
  char buf[BUFLEN];
  buf=argv[3];
  error: assignment to expression with array type
  The error message is telling you that you can't assign to an array. For example,
  char foo[4];
  foo = "bar";        // This is an error
  strcpy(foo, "bar"); // This is fine and dandy, like sour candy...
  A "string" in C is a data layout, not a data type; it's defined as "a contiguous sequence of characters terminated by and including the first null character". An array of char may or may not contain a string, and the compiler isn't going to assume that it will hold a string. String literals are (almost always) null-terminated; character arrays are not

  Note that the same happens if you tell the compiler that you have an array of the right>  void load_buffer(char buffer[100]) {
  /* prints 4 too! */

  printf("sizeof(buffer): %d\n",>  }
  An array as parameter is just declaring a pointer. The compiler automatically changes that to char *name even if it was declared as char name[N].

  If you want to force callers to pass an array of>  void load_buffer(char (*buffer)[100]) {
  /* prints 100 */

  printf("sizeof(buffer): %d\n",>  }
  It's a pointer to the array you have in main, so you need to dereference in the function to get the array. Indexing then is done by
  buffer[0][N] or (*buffer)[N]
  Nobody I know is doing that and I'm neither doing it myself, because it rather complicates passing of the argument. But it's good to know about it. You can call the function like this then
  load_buffer(&buffer)

  If you want to accept other>  So can use strlen which is easier:
  strlen scans for the first '\0' null character. Since the contents of test_string are garbage, the behavior is undefined. It might return a small value if there happens to be a null character, or a large value or program crash if there don't happen to be any zero bytes
  6.AF and PF
  AF_* stands for Address Family
  PF_* stands for Protocol Family
  BSD man page promises:
  "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere.
  Even linux/socket.h specifies as
  #define PF_INET AF_INET
  Yet, we can say
  int socket(int family,int type,int prototype);

  Here,family>  7.ssize_t

  This data type is used to represent the>  8.read() and write()
  read()
  #include  
  int  read(  int  handle,  void  *buffer,  int  nbyte );
  The read() function attempts to read nbytes from the file associated with handle, and places the characters read into buffer. If the file is opened using O_TEXT, it removes carriage returns and detects the end of the file.
  The function returns the number of bytes read. On end-of-file, 0 is returned, on error it returns -1, setting errno to indicate the type of error that occurred.
  write()
  #include  
  int  write(  int  handle,  void  *buffer,  int  nbyte  );
  The write() function attempts to write nbytes from buffer to the file associated with handle. On text files, it expands each LF to a CR/LF.
  The function returns the number of bytes written to the file. A return value of -1 indicates an error, with errno set appropriately.
  9.\n \r
  A line feed means moving one line forward. The code is \n. A carriage return means moving the cursor to the beginning of the line. The code is \r
  sprintf(strings,"%s\r\n",ptr);//can append \r\n to string
  receive data from client till receive '\n'

  ssize_t readline_unbuffered (int fd, void *vptr,>  {
  int n, rc;
  char c, *ptr;
  ptr = vptr;
  for (n=1; n  11.format
  scanf("%c",&ptr[1]);can't use c,cz it will read only one character
  //scanf("%s",ptr[1]);warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat],cz this may regard it as an offset,which is recognized as int
  /*scanf("%s",&ptr[2]);//use "%s",the compiler automatically transfer it into ASCII from decimal
  scanf("%s",&ptr[3]);
  scanf("%s",&ptr[4]);*///can't use this cz if write 111 to ptr[2],will fill in ptr[2],ptr[3],ptr[4]
  12.to install wireshark
  for unknown reason,can't install directly from software center and terminal,then follow instructions to install from source online still have many errors then check online using like --with --without,but finally after "make" step,still can't run "make install"
  successfully,but then found can install from software center and terminal....
  13.trace
  #ifdef TRACE   //if define TRACE ,means gcc -DTRACE
  #define trace(x) x
  #else          //without defining TRACE
  #define trace(x)
  #endif
  trace() is a MACRO function,which can be used for  debugging
  trace(err_msg("(%s)-No response received after %d seconds",prog_name,TIMEOUT));
  



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-624604-1-1.html 上篇帖子: Java连接Azure SQL Database 下篇帖子: php函数mysql_query批量执行多句sql语句
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表