Esempio n. 1
0
int	get_weather(char *code)
{
  char	*url;
  int	fd;
  int	result;

  result = 4242;
  dl_file(code);
  fd = open("/tmp/mein_weather.txt", O_RDONLY);
  while ((url = mein_getnextline(fd)) != NULL &&
	 fd != -1 && result == 4242)
    {
      if (match("Temperature:*", url))
	result = extract_weather(url);
      xfree(url);
    }
  xfree (url);
  xclose(fd);
  init_buffer();
  return (result);
}
Esempio n. 2
0
int main(){
  // Initialize Winsock
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup function failed with error: %d\n", iResult);
        return 1;
    }
    //----------------------
    // Create a SOCKET for connecting to server
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    printf("Enter the IP address to connect to: ");
    string ipAddr;
    getline(cin, ipAddr);
    clientService.sin_addr.s_addr = inet_addr(ipAddr.c_str());
    clientService.sin_port = htons(31000);

    //----------------------
    // Connect to server.
    iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService));
    if (iResult == SOCKET_ERROR) {
        wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
        iResult = closesocket(ConnectSocket);
        if (iResult == SOCKET_ERROR)
            wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // Begin talking with Server here.
    char recvBuffer[BUF_SIZE];
    char sendBuffer[BUF_SIZE];
    string userInput;
    string userOpt;

    wprintf(L"Connected to server.\n");
    printf("Accepting data from server\n");

    do {
        print_options();

        printf("\nEnter a command to execute: ");
        memset(&sendBuffer, 0, sizeof(sendBuffer));
        memset(&recvBuffer, 0, sizeof(recvBuffer));
        getline(cin, userOpt);
        strcat(sendBuffer, userOpt.c_str());
        send_data(ConnectSocket, sendBuffer);

        switch(atoi(userOpt.c_str())){
            case 1:{
                get_output(ConnectSocket, recvBuffer).c_str();
                break;
            }
            case 2:{
                printf("Enter the filename: ");
                getline(cin,userInput);
                memset(&sendBuffer, 0, sizeof(sendBuffer));
                strcat(sendBuffer, userInput.c_str());
                //send filename
                send_data(ConnectSocket, sendBuffer);
                //recv file data
                memset(&recvBuffer, 0, sizeof(recvBuffer));
                string data = get_output(ConnectSocket,recvBuffer);

                ofstream myfile;
                myfile.open(userInput.c_str());
                myfile << data;
                break;
            }
            case 3:{
                printf("Enter the filename: ");
                getline(cin,userInput);
                memset(&sendBuffer, 0, sizeof(sendBuffer));
                strcat(sendBuffer, userInput.c_str());
                //send filename
                send_data(ConnectSocket, sendBuffer);
                //Get file data
                dl_file(ConnectSocket, userInput);

                send_data(ConnectSocket, "<END>");

                break;
            }
            case 4:{
                printf("Enter command: ");
                getline(cin,userInput);
                memset(&sendBuffer, 0, sizeof(sendBuffer));
                strcat(sendBuffer, userInput.c_str());
                send_data(ConnectSocket, sendBuffer);
                get_output(ConnectSocket, recvBuffer).c_str();
                break;
            }
            case 5:{
                get_output(ConnectSocket, recvBuffer).c_str();
                break;
            }
            case 6:{
                get_output(ConnectSocket, recvBuffer).c_str();
                break;
            }
            case 7:{
                get_output(ConnectSocket, recvBuffer).c_str();
                break;
            }
        }

    }while (atoi(userOpt.c_str()) != 0);

    // End connection with Server
    wprintf(L"Closing socket connection.\n");
    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    WSACleanup();
    return 0;
}
Esempio n. 3
0
int main(int argc , char* argv[])//exe ip port
{
	signal(13,SIG_IGN);
	signal(17,sig_handle);
	if(argc != 3)
	{
		printf("usge : exe ip port");
		exit(-1);
	}
	int fd_server ,fd_client;
	struct sockaddr_in client_addr ;
	int addrlen = sizeof(client_addr);
	fd_server = listenfd_init(argv[1],argv[2]);
	while( (fd_client = accept(fd_server,(struct sockaddr*)&client_addr,&addrlen) ) > 0 )
	{
		if(fd_client == -1)
		{
			if(errno == EINTR)
			{
				continue ;
			}
		}
		printf("a client connect :%s:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
		if(fork() == 0)
		{
			close(fd_server);
			char comd[1024];
			//circle recv msg
			while(1)
			{
				fflush(stdout);
				char sign[32];
				bzero(comd, 1024);
				recv_command(fd_client,comd);
				
				printf("comd:%s\n", comd);
				if(strncmp(comd, "ls", 2) == 0)
				{
					send_dir(fd_client);
				}
				else if(strncmp(comd, "download", 8) == 0)
				{
					ul_file(fd_client);
				}
				else if(strncmp(comd, "cd", 2) == 0)	
				{
					send_path(fd_client, comd);		
				}
				else if(strncmp(comd, "rm", 2) == 0)
				{
					system(comd);
				}
				else if(strncmp(comd, "upload", 6) == 0)
				{
					dl_file(fd_client);
				}
				else if(strncmp(comd, "cl", 2) == 0)
				{
					system("clear");
				}

			}
			exit(0);
		}
	}

}