Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSO1 - Socket Chat CSO1 – Socket Chat home help policies quizzes schedule submissions Table of Contents 1 Requirements 2 Undefined behaviors 3 Example 4 Tips 4.1 This assignment requires looking things up 4.2 Testing on a non-server 4.3 Use lldb 4.4 Feedback In this assignment, you’ll take the server code we supplied and the client code you wrote in lab 9 and combine them to make a command-line 2-party chat program. 1 Requirements Write a single file called schat.c. Use only C library functions and code you wrote in it: no third-party libraries or C++. schat.c should have a main function that accepts command-line arguments. If given no arguments, it should run in server mode; if given two arguments (the first an IP address, the second a port number) it should run in client mode, connecting to that server. The server should begin by Pick a random ephemeral-range port, like the lab server code did Display that port number to the terminal, like the lab server code did Listen for IPv4 connections on any IP address, like the lab server code did accept only once, unlike the lab server’s accept loop close the listener socket before working with the accepted socket The client should begin by connecting to the given IP address and port, like your lab client did Both should then proceed as follows: Repeatedly (i.e., in an infinite loop) use poll1 to pick either the connected socket (from the server’s accept or the client’s connect) or the standard input stream2 to read3 from. Use a 1-minute4 timeout for poll. If poll returns a positive number (i.e., it succeeded), If the standard input has revents including POLLIN, read from standard input and write what you read to the socket. If the socket has revents including POLLIN, read from the socket and write what you read to standard output5. You may assume no single message is more than 4096 bytes, to avoid needing to loop your read/write calls. 2 Undefined behaviors This assignment does not require you to handle the following in any particular way: Giving notice of server IP address Failure of any of the socket, read, or write instructions Handling the closing of the remote connection Stopping if the poll call times out Having any way to end the program other than Ctrl+C Avoiding memory leaks Clean display when the remote message arrives while the user it typing Displaying who sent which message You do need to avoid buffer overflows, use-after-free, and other memory bugs. You are also invited to add sane behaviors for all of the above cases, but are not required to do so. 3 Example In a simple implementation, once the client and server connect everything that is typed in one will appear in the other after you press enter. Server Client $ $ As a reminder, you don’t need to be exactly like the above; the initial display, format of output, and behavior of the server after the client ends are all undefined by this assignment. 4 Tips 4.1 This assignment requires looking things up You will notice all we said about poll was check the manual page. This is by design. We hope you now know enough to learn a bit on your own and use it. It is also possible to look things up online. Except for online man-pages6, this is a bad idea. In general, online example code is explaining something nuanced within a specific context, and it takes some experience before you are able to tease out the part you can use from the context it is discussed within. I expect if you go to a search engine to find example code, you’ll end up with a lot of code that does not work or meet the required specification in some nuanced way. 4.2 Testing on a non-server The CS department servers (notably including servers portal redirects you to) all have static IP Addresses meaning tools like host can learn who they are. Your laptop probably does not. If your laptop has a C compiler and poll, socket, etc., implemented, you can still test locally but you’ll need to use the IP address 127.0.0.1 which always means talk to myself, running the server and client in two different terminal windows at the same time. 4.3 Use lldb When you get a segfault, you should recompile with clang -g schat.c load in a debugger with lldb a.out run and see the line of code that segfaulted This will be far easier than trying to locate the segfault without a debugger. You may also find a debugger useful for other kinds of errors. 4.4 Feedback We do not currently have plans to offer automated feedback on your code when you submit. We’re also not planning on testing strange cases: if you can carry on a chat conversation with someone else, where either one of you can type several things in a row and it works, you should be good to go. see man 3 poll which has some example code in it. There’s also a man 2 poll; you want man 3 poll instead↩︎ which is always already opened as file descriptor 0↩︎ Note: this means you’ll need to use POLLIN as the fds[i].events instead of the POLLOUT | POLLWRBAND used in the manual page example.↩︎ 60 thousand milliseconds↩︎ which is always already opened as file descriptor 1↩︎ And even those vary a lot in how up-to-date they are…↩︎ Copyright © 2021 Luther Tychonievich. Released under the CC-BY-NC-SA 4.0 license. Last updated 2021-10-25 15:58