示例#1
0
/*-----------------------------------------------------------------------
 *
 * Program: chatclient
 * Purpose: contact a chatserver and allow users to chat
 * Usage:   chatclient <compname> <appnum>
 *
 *-----------------------------------------------------------------------
 */
int
main(int argc, char *argv[])
{
	computer	comp;
	connection	conn;
	char		buff[BUFFSIZE];
	int		len;

	if (argc != 3) {
		(void) fprintf(stderr, "usage: %s <compname> <appnum>\n",
			       argv[0]);
		exit(1);
	}

	/* convert the compname to binary form comp */

	comp = cname_to_comp(argv[1]);
	if (comp == -1)
		exit(1);

	/* make a connection to the chatserver */

	conn = make_contact(comp, (appnum) atoi(argv[2]));
	if (conn < 0) 
		exit(1);

	(void) printf("Chat Connection Established.\n");
	(void) printf(INPUT_PROMPT);
	(void) fflush(stdout);

	/* iterate, reading from local user and then from chatserver */

	while((len = readln(buff, BUFFSIZE)) > 0) {
		buff[len - 1] = '\n';
		(void) send(conn, buff, len, 0);
		
		/* receive and print a line from the chatserver */
		if ((len = recvln(conn, buff, BUFFSIZE)) < 1)
			break;
		(void) printf(RECEIVED_PROMPT);
		(void) fflush(stdout);
		(void) write(STDOUT_FILENO, buff, len);

		(void) printf(INPUT_PROMPT);
		(void) fflush(stdout);
	}

	/* iteration ends when stdin or the connection indicates EOF */

	(void) printf("\nChat Connection Closed.\n");
	(void) send_eof(conn);
	exit(0);
}
示例#2
0
/*-----------------------------------------------------------------------
 *
 * Program: echoclient
 * Purpose: contact echoserver, send user input and print server response
 * Usage:   echoclient <compname> [appnum]
 * Note:    Appnum is optional. If not specified the standard echo appnum
 *          (7) is used.
 *
 *-----------------------------------------------------------------------
 */
int
main(int argc, char *argv[])
{
    computer	comp;
    appnum		app;
    connection	conn;
    char		buff[BUFFSIZE];
    int		expect, received, len;

    if (argc < 2 || argc > 3) {
        (void) fprintf(stderr, "usage: %s <compname> [appnum]\n",
                       argv[0]);
        exit(1);
    }

    /* convert the arguments to binary format comp and appnum */

    comp = cname_to_comp(argv[1]);
    if (comp == -1)
        exit(1);

    if (argc == 3)
        app = (appnum) atoi(argv[2]);
    else if ((app = appname_to_appnum("echo")) == -1)
        exit(1);

    /* form a connection with the echoserver */

    conn = make_contact(comp, app);
    if (conn < 0)
        exit(1);

    (void) printf(INPUT_PROMPT);
    (void) fflush(stdout);

    /* iterate: read input from the user, send to the server,	*/
    /*	    receive reply from the server, and display for user */

    while((len = readln(buff, BUFFSIZE)) > 0) {

        /* send the input to the echoserver */

        (void) send(conn, buff, len, 0);
        (void) printf(RECEIVED_PROMPT);
        (void) fflush(stdout);

        /* read and print same no. of bytes from echo server */

        expect = len;
        for (received = 0; received < expect;) {
            len = recv(conn, buff, (expect - received) < BUFFSIZE ?
                       (expect - received) : BUFFSIZE, 0);
            if (len < 0) {
                send_eof(conn);
                return 1;
            }
            (void) write(STDOUT_FILENO, buff, len);
            received += len;
        }
        (void) printf("\n");
        (void) printf(INPUT_PROMPT);
        (void) fflush(stdout);
    }

    /* iteration ends when EOF found on stdin */

    (void) send_eof(conn);
    (void) printf("\n");
    return 0;
}
示例#3
0
int main(int argc, char *argv[])
{
  // valid input?
  if(argc <= 2){
    printf("usage: echoclient <computername> <appnum>\n");
    return 0;
  }

  // this is the host - ip for example. looks strange, is aber so.
  computer serverComputer;
  // application number - this directly translates to port.
  appnum applicationIdentifier;
  // probably something from netinet in a new struct?
  connection serverConnection;

  // get server name
  serverComputer = cname_to_comp(argv[1]);

  if(serverComputer == -1){
    printf("Bad host entered, try 127.0.0.1\n");
    return 0;
  }

  // get application id
  applicationIdentifier = atoi(argv[2]);

  //try to contact the server
  serverConnection = make_contact(
                        serverComputer, 
                        applicationIdentifier);

  // check connection validity
  if(serverConnection == -1){
    printf("could not connect\n");
    return 0;
  }
  else
    printf("connection established, id: %i\n", serverConnection);

  // used for input from stdin
  char userBuffer[1024];
  size_t userMsgLength;
  
  // used for input from network
  char serverBuffer[1024];  
  int serverMsgLength;
  
  while(1){
    // check stdin for new input
    if((userMsgLength = read(0, userBuffer, 1024)) > 0){
      // write to connection
      write(serverConnection, userBuffer, userMsgLength);
    }

    // check network for new output
    if((serverMsgLength = read(serverConnection, serverBuffer, sizeof(serverBuffer) - 1)) > 0){
      printf("Echo from server:\n");

      // welcome to c - end string
      serverBuffer[serverMsgLength] = 0;

      // write server input to stdout
      fputs(serverBuffer, stdout);
    }
  }
  // write(serverConnection, "test", 4);
	return 0;
}