Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as C by michauwilliam ( 15 years ago )
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <iostream>
#include <pthread.h>
#include <ncurses.h>

#define PORT 4322

int CURS_X = 1;
int CURS_Y = 1;

int main(int argc, char** argv)
{
  // networking stuff
  int s, n;
  int max = 1024;
  char buf[max+1];
  struct sockaddr_in serwer_adres;
  if( (s = socket(AF_INET,SOCK_STREAM,0)) <0 )
  {
    printf("Nie udalo sie utworzyc gniazdka");
    exit(1);
  }
  bzero(&serwer;_adres,sizeof(serwer_adres));
  serwer_adres.sin_family = AF_INET;
  serwer_adres.sin_port = htons(PORT); // serwer nam sie przedstawi
  if (inet_pton(AF_INET,argv[2],&serwer;_adres.sin_addr)<=0){
    printf("Podaj prawidlowy adres internetowy\n");
    exit(1);
  }

  if(connect(s,(sockaddr *)&serwer;_adres,sizeof(serwer_adres))<0)
  {
    printf("Nie udalo sie polaczyc");
    exit(1);
  }

  n = write(s,argv[1],strlen(argv[1])); // send login to the server

  // networking stuff
  //
  // graphics stuff
  WINDOW* input_window;
  WINDOW* output_window;
  WINDOW* status_window;

  initscr();
  refresh();
  echo();

  int max_x = COLS-1;
  int max_y = LINES-1;

  // tworzymy okna
  input_window = newwin(max_y,max_x/3+3,0,0);
  box(input_window, 0, 0);
  mvwprintw(input_window, 0, 0, "%s", "input window");
  wrefresh(input_window);

  output_window = newwin(max_y,max_x/3+3,0,max_x/3+3);
  box(output_window, 0, 0);
  mvwprintw(output_window, 0, 0, "%s", "output window");
  wrefresh(output_window);

  status_window = newwin(max_y,max_x/3-6,0,max_x/3*2+6);
  box(status_window, 0, 0);
  mvwprintw(status_window, 0, 0, "%s", "status window");
  wrefresh(status_window);

  move(CURS_Y,CURS_X);
  refresh();

// networking again

  char message[1024];
  fd_set master;
  fd_set read_fds;

  FD_ZERO(&master;);
  FD_ZERO(&read;_fds);

  FD_SET(0,&master;);
  FD_SET(s,&master;);

  int i = 0;

  while(true){
    read_fds = master;
    if (select(2,&read;_fds,NULL,NULL,NULL) == -1){
      perror("select:");
      exit(1);
    }
    // if there are any data ready to read from the socket
    if (FD_ISSET(s, &read;_fds)){
      exit(1);
      n = read(s,buf,max);
      buf[n]=0;
      if(n<0)
      {
        printf("Blad odczytu z gniazdka");
        exit(1);
      } 
      mvwprintw(output_window,1,1,"%s\n",buf);
    }
    // if there is something in stdin
    if (FD_ISSET(0, &read;_fds)){
      getstr(message);
      move(CURS_Y++,CURS_X);
      if (CURS_Y == LINES-2){
        CURS_Y = 1;
      }
      n = write(s,message,strlen(message));
      if (n < 0){
        perror("writeThread:");
        exit(1);
      }
    }
  }
  close(s);
  delwin(input_window);
  delwin(output_window);
  delwin(status_window);
  endwin();
  return 0;
}

 

Revise this Paste

Children: 112882
Your Name: Code Language: