#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
SOCKET Socket;
SOCKADDR_IN addr;
void gotoxy(int xpos, int ypos)
{
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
int main()
{
string m_host = "192.168.178.99";
string m_port = "50290";
WSADATA wsa;
int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
if(rc == SOCKET_ERROR){
cout << "Error while starting server... \n";
}
Socket = socket(AF_INET, SOCK_STREAM, 0);
if(Socket == INVALID_SOCKET){
std::cout << "Error while building socket. \n";
}
addr.sin_addr.s_addr = inet_addr(m_host.c_str());
addr.sin_port = htons(atoi(m_port.c_str()));
addr.sin_family = AF_INET;
if(connect(Socket, (SOCKADDR*)&addr, sizeof(SOCKADDR)) == 0){
cout << "Connection with server " << inet_ntoa(addr.sin_addr)
<< " established." << endl << endl;
} else {
cout << "Impossible to connect to server " << inet_ntoa(addr.sin_addr)
<< ":" << addr.sin_port
<< ". Please check host and port. \n";
return 1;
}
// Everything okay - start communicating with server
char buffersend[1024];
char bufferrecv[1024];
int bufpos = 0;
int bufIndex = 0;
char c;
fd_set fdSetRead;
TIMEVAL timeout;
bool mainloop = true;
getch();
system("cls");
char outport11[] = "setport 1.1\r\n";
char outport10[] = "setport 1.0\r\n";
char outport21[] = "setport 2.1\r\n";
char outport20[] = "setport 2.0\r\n";
char outport31[] = "setport 3.1\r\n";
char outport30[] = "setport 3.0\r\n";
char outport41[] = "setport 4.1\r\n";
char outport40[] = "setport 4.0\r\n";
char outport51[] = "setport 5.1\r\n";
char outport50[] = "setport 5.0\r\n";
char outport61[] = "setport 6.1\r\n";
char outport60[] = "setport 6.0\r\n";
char outport71[] = "setport 7.1\r\n";
char outport70[] = "setport 7.0\r\n";
char outport81[] = "setport 8.1\r\n";
char outport80[] = "setport 8.0\r\n";
char eingabe = 0;
int status1 = 0,
status2 = 0,
status3 = 0,
status4 = 0,
status5 = 0,
status6 = 0,
status7 = 0,
status8 = 0;
send(Socket, outport10, strlen(outport10), 0);
Sleep(1);
send(Socket, outport20, strlen(outport20), 0);
Sleep(1);
send(Socket, outport30, strlen(outport30), 0);
Sleep(1);
send(Socket, outport40, strlen(outport40), 0);
Sleep(1);
send(Socket, outport50, strlen(outport50), 0);
Sleep(1);
send(Socket, outport60, strlen(outport60), 0);
Sleep(1);
send(Socket, outport70, strlen(outport70), 0);
Sleep(1);
send(Socket, outport80, strlen(outport80), 0);
printf("Software: Finnex \n");
printf("Erstellt fuer AVR IO von Pollin");
printf("\n\nAnzeige: K1 K2 K3 K4 K5 K6 K7 K8");
gotoxy(10,4);
printf("O O O O O O O O ");
printf("\n\n\n\n\nSteuerung:\n K1=1\n K2=2\n K3=3\n K4=4\n K5=5\n K6=6\n K7=7\n K8=8\n 9=Alles auf Null setzen");
while(mainloop)
{
//SEND DATA
eingabe = getch();
if(eingabe == 49 && status1 != 1)
{
send(Socket, outport11, strlen(outport11), 0);
status1 = 1;
eingabe = 0;
}
if(eingabe == 49 && status1 == 1)
{
send(Socket, outport10, strlen(outport10), 0);
status1 = 0;
eingabe = 0;
}
gotoxy(10,4);
if (status1 == 0)
{
printf("O");
}
if (status1 == 1)
{
printf("X");
}
if(eingabe == 50 && status2 == 0)
{
send(Socket, outport21, strlen(outport21), 0);
status2 = 1;
eingabe = 0;
}
if(eingabe == 50 && status2 == 1)
{
send(Socket, outport20, strlen(outport20), 0);
status2 = 0;
eingabe = 0;
}
gotoxy(15,4);
if (status2 == 0)
{
printf("O");
}
if (status2 == 1)
{
printf("X");
}
if(eingabe == 51 && status3 == 0)
{
send(Socket, outport31, strlen(outport31), 0);
status3 = 1;
eingabe = 0;
}
if(eingabe == 51 && status3 == 1)
{
send(Socket, outport30, strlen(outport30), 0);
status3 = 0;
eingabe = 0;
}
gotoxy(20,4);
if (status3 == 0)
{
printf("O");
}
if (status3 == 1)
{
printf("X");
}
if(eingabe == 52 && status4 == 0)
{
send(Socket, outport41, strlen(outport41), 0);
status4 = 1;
eingabe = 0;
}
if(eingabe == 52 && status4 == 1)
{
send(Socket, outport40, strlen(outport40), 0);
status4 = 0;
eingabe = 0;
}
gotoxy(25,4);
if (status4 == 0)
{
printf("O");
}
if (status4 == 1)
{
printf("X");
}
if(eingabe == 53 && status5 == 0)
{
send(Socket, outport51, strlen(outport51), 0);
status5 = 1;
eingabe = 0;
}
if(eingabe == 53 && status5 == 1)
{
send(Socket, outport50, strlen(outport50), 0);
status5 = 0;
eingabe = 0;
}
gotoxy(30,4);
if (status5 == 0)
{
printf("O");
}
if (status5 == 1)
{
printf("X");
}
if(eingabe == 54 && status6 == 0)
{
send(Socket, outport61, strlen(outport61), 0);
status6 = 1;
eingabe = 0;
}
if(eingabe == 54 && status6 == 1)
{
send(Socket, outport60, strlen(outport60), 0);
status6 = 0;
eingabe = 0;
}
gotoxy(35,4);
if (status6 == 0)
{
printf("O");
}
if (status6 == 1)
{
printf("X");
}
if(eingabe == 55 && status7 == 0)
{
send(Socket, outport71, strlen(outport71), 0);
status7 = 1;
eingabe = 0;
}
if(eingabe == 55 && status7 == 1)
{
send(Socket, outport70, strlen(outport70), 0);
status7 = 0;
eingabe = 0;
}
gotoxy(40,4);
if (status7 == 0)
{
printf("O");
}
if (status7 == 1)
{
printf("X");
}
if(eingabe == 56 && status8 == 0)
{
send(Socket, outport81, strlen(outport81), 0);
status8 = 1;
eingabe = 0;
}
if(eingabe == 56 && status8 == 1)
{
send(Socket, outport80, strlen(outport80), 0);
status8 = 0;
eingabe = 0;
}
gotoxy(45,4);
if (status8 == 0)
{
printf("O");
}
if (status8 == 1)
{
printf("X");
}
if(eingabe == 57)
{
send(Socket, outport10, strlen(outport10), 0);
Sleep(1);
send(Socket, outport20, strlen(outport20), 0);
Sleep(1);
send(Socket, outport30, strlen(outport30), 0);
Sleep(1);
send(Socket, outport40, strlen(outport40), 0);
Sleep(1);
send(Socket, outport50, strlen(outport50), 0);
Sleep(1);
send(Socket, outport60, strlen(outport60), 0);
Sleep(1);
send(Socket, outport70, strlen(outport70), 0);
Sleep(1);
send(Socket, outport80, strlen(outport80), 0);
eingabe = 0;
status1=0;
status2=0;
status3=0;
status4=0;
status5=0;
status6=0;
status7=0;
status8=0;
gotoxy(10,4);
printf("O O O O O O O O ");
}
}
cout << "Shutting down client. \n";
closesocket(Socket);
WSACleanup();
cout << "\nPROGRAM EXIT";
}