Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Last active November 7, 2017 11:02
Show Gist options
  • Save iKlotho/bf7b48e13cc3c7574a4a to your computer and use it in GitHub Desktop.
Save iKlotho/bf7b48e13cc3c7574a4a to your computer and use it in GitHub Desktop.
C Language Translate Console Program
/*++
Module Name:
main.c
Vers : v1
--*/
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
// Standart Libraries
#include <windows.h>
#include <iostream>
#include <locale.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <tchar.h>
#include <thread>
#include <string>
#include <time.h>
using namespace std;
//----------------------------------------//
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define HOSTNAME "translate.google.com"
short port = 80;
char request[4096] =
"GET /translate_a/t?client=t&text=%s&hl=en&sl=en&tl=tr HTTP/1.1\r\n"
"Host: translate.google.com\r\n"
"User-Agent: App/1.7\r\n\r\n";
SOCKET ConnectedSock(const char* hostname, short port)
{
SOCKET client = 0;
struct hostent *host = NULL;
struct sockaddr_in client = { 0 };
if (!(host = gethostbyname(hostname)))
{
std::cout << "HOST error : " << WSAGetLastError() << std::endl;
return INVALID_SOCKET;
}
client.sin_family = AF_INET;
client.sin_port = htons((short)port);
client.sin_addr = *((struct in_addr *)host->h_addr);
if (INVALID_SOCKET == (client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)))
{
std::cout << "SOCKET error : " << WSAGetLastError() << std::endl;
return INVALID_SOCKET;
}
if (connect(client, (struct sockaddr *)&info, sizeof(struct sockaddr)))
{
std::cout << "CONNECT error : " << WSAGetLastError() << std::endl;
closesocket(client);
return INVALID_SOCKET;
}
return client;
}
int main()
{
setlocale(LC_ALL, "turkish"); // türkce karakterler icin
int i,j,len;
char name[100];
char buffer[32 * 1024] = { 0 };
char shadow[1024] = { 0 };
char buf[32 * 1024] = { 0 };
char *str, *ptr;
WSADATA wsaData;
WCHAR shadowU[1024] = { 0 };
SOCKET sock = INVALID_SOCKET;
WSAStartup(MAKEWORD(2, 2), &wsaData);
sock = ConnectedSock(HOSTNAME, 80);
if (sock == INVALID_SOCKET) {
std::cout << "Socket error : " << WSAGetLastError() << std::endl;
}
printf("Word :");
scanf("%[^\n]%*c", name);
// request'teki text=%s kısmına kullanıcıdan gelen name degıskenı atanır.
len = sprintf(buf, request, name);
send(sock, buf, len, 0); // cevirelecek metni gonder
len = 0;
// ]'a kadar oku
while (TRUE)
{
int ret = 0;
ret = recv(sock, buffer + len, sizeof(buffer) - len, 0);
if (ret <= 0) {
break;
}
if (strstr(buffer, "]")){
break;
}
len += ret;
}
closesocket(sock);
str = buffer;
// [[["çeviri buraya geldi"
while (TRUE)
{
str = strstr(str, "[[[\"");
if (str == NULL) {
break;
}
ptr = strstr(str, "\",");
if (ptr == NULL) {
break;
}
*ptr = 0;
sprintf(shadow + strlen(shadow), "%s ", str + strlen("[[[\""));
str = ptr + 1;
}
printf("Response : %s", shadow);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment