Am writing an application for a Point of Sale TERMINAL , the manual for the terminal restricts direct MYSQL connection from the terminal, All connections should be done using Sockets. How can I connect to a MYSQL database using C sockets? Below is my working C program that establishes a TCP connection to specified ip and port.
/*
* Create a TCP socket
* #Author Salim Said
* Jan 2 2015 17:00hrs
*/
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected successfully");
return 0;
}
Related
I am trying to make a TCP Socket Server in C that stores the data that the clients are sending to the server in a struct array. Well, so far, so good. When i am printing the values of the struct, only the last value(key) is stored in all the positions of the struct array!
Why is that? Thanks in advance!
Client Code:
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
char *p;
server.sin_addr.s_addr = inet_addr(argv[1]);
int port = strtol(argv[2], &p, 10);
server.sin_port = htons(port);
server.sin_family = AF_INET;
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
write(sock , argv[3] ,strlen(argv[3])+1); //send the 3rd argument(which is the input key)
close(sock);
return 0;
}
Server Code:
struct key_store
{
char *key;
}store[1024];
int STORED=0;
void handler(int socket_desc)
{
int client_sock;
char client_message[1024];
client_sock = accept(socket_desc, (struct sockaddr *) NULL, NULL);
printf("Connection accepted\n");
while( read(client_sock , client_message , 100) > 0 )
{
store[STORED].key=client_message;
STORED++;
}
close(client_sock);
}
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
server.sin_port = htons( 8888 );
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
printf("Socket created\n");
//Prepare the sockaddr_in structure
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
printf("bind done\n");
//Listen
listen(socket_desc , 5);
int count =0;
while(count < 3)
{
handler(socket_desc);
count++;
}
close(socket_desc);
for (int i=0;i<STORED;i++)
{
printf("KEY --> %s\n",store[i].key); //print the store keys
}
return 0;
}
Input:
./client 127.0.0.1 8888 123456
./client 127.0.0.1 8888 testkey
./client 127.0.0.1 8888 2017
Output:
./server
Socket created
bind done
Connection accepted
Connection accepted
Connection accepted
KEY --> 2017
KEY --> 2017
KEY --> 2017
you are storing a pointer to client_message in your store. Next message will overwrite that buffer. You have to make a copy of the message before you store it
try
put(strdup(client_message));
You have all sorts of other issues, but this will at least move you forward
It is a sample code where i have tried to pass a request to server and get me the desired data.
But my connect to remote server code is not working as i thought.
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
I don't know why value of the condition is getting below zero.Why connection is not getting done?
Full code:
/*
Create a TCP socket
*/
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message , server_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
//Send some data
message = "GET / HTTP/1.1\r\n\r\n";
if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("Data Send\n");
//Receive a reply from the server
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}
puts("Reply received\n");
//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
puts(server_reply);
closesocket(s);
WSACleanup();
return 0;
}
You need to initalize the whole sockaddr_in to zero before assigning to it.
memset( &server, 0, sizeof(server) );
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
I copied your code and tried it out and it seemed to work, maybe you have a firewall that is interfering.
BTW I changed your inet_addr call to
InetPtonA( AF_INET, "74.125.235.20", &server.sin_addr.s_addr );
Hi I'm new to socket programming and I'm trying out the following code from the tutorial of http://www.binarytides.com/winsock-socket-programming-tutorial/
I'm trying to connect to server and I'm using the IP address of google. Here is the code:
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
return 0;
}
So far socket can be created but I can't connect to the server. To be more specific I always exit and have following:
The program '[2060] SocketCTest.exe: Native' has exited with code 1 (0x1).
even if I set a breakpoint before returns.
There's no error. I've tried on my computer and connect returned with error (WSAETIMEDOUT). I'm not sure whether there are the network settings on my computer, proxies, firewalls, and so on; or that google host is configured not to accept direct socket connections.
Anyway here's your code with some small adjustments:
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc, char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char c = 0;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
printf("Failed. Error Code : %d.\nPress a key to exit...", WSAGetLastError());
c = getch();
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Could not create socket : %d.\n", WSAGetLastError());
WSACleanup();
c = getch();
return 1;
}
printf("Socket created. Connecting...\n");
memset(&server, 0, sizeof server);
server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect error:%d.\nPress a key to exit...", WSAGetLastError());
closesocket(s);
WSACleanup();
c = getch();
return 1;
}
puts("Connected.\nPress a key to exit...");
closesocket(s);
WSACleanup();
c = getch();
return 0;
}
Now if you want to see the code actually connecting i'd suggest using localhost (127.0.0.1) instead of 74.125.224.72, and the port 3389 for example (it's the Remote Desktop Server (RDP) port, if you have RDP configured and running on your computer) instead of 80; or you could let 80 if you have a web server (IIS?) running on your computer.
To get a list of server programs that run on your computer run the command:
`netstat -an | findstr LISTEN`
which would output a bunch of lines (and the ones that we care about are) in this form (here's the one corresponding to the RDP example from above):
`TCP 127.0.0.1:3389 0.0.0.0:0 LISTENING`
I made a Server in C on port 100. The problem is that if anyone out of my PC writes telnet myip 100 he can't connect to my server!
Code:
#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include "ws2tcpip.h"
#pragma comment(lib,"ws2_32.lib")
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c;
char *message;
printf("\nInitialising Winsock...\n");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Winsock initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket: %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(100);
int inet_pton(int af, const char *src, void *dst);
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code: %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(s, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
{
puts("Connection accepted");
//Reply to the client
message = "Hello Client, I have received your connection. But I have to go now... bye!\n";
send(new_socket, message, strlen(message) , 0);
}
if (new_socket == INVALID_SOCKET)
{
printf("Accept failed with error code: %d" , WSAGetLastError());
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
My provider is Fastweb (i'm from italy, i don't know if you know it).
What's the problem? The source code of the server or the provider?
Thank you a lot!
I'm trying to program a simple client and server using sockets in C. Both the client and server are running Ubuntu. The server is broadcasting an ad-hoc network that the however I cannot seem to get the client to connect to the server despite being able to ping it from terminal.
The code I'm using for the server and client are adapted from an online source and are as such:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Reply to the client
message = "Hello Client , I have received your connection. But I have to go now, bye\n";
write(new_socket , message , strlen(message));
return 0;
}
Client
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *message;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
server.sin_addr.s_addr = inet_addr("192.168.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected\n");
//Send some data
message = "GET / HTTP/1.1\r\n\r\n";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("Data Send\n");
return 0;
}
The IP address was obtained by running ifconfig on the terminal of the server and looking at its inet addr value.
Additionally I've also disabled the firewall on both the client and the server by running sudo ufw disable in the terminal. Errno also outputs connection refused.
Can anyone please help me with this?
Your server listens to port 8888 and client tries to connect to port 80, both server.sin_port = htons( x ); must use the same port number.
Instead of directly using inet_addr("192.168.0.1");
use
struct sockaddr_in6 ser_address;
inet_pton(AF_INET,"192.168.0.1",&ser_address.sin.addr);
Note: We cannot use presentation style address in the codes when passing it to socket functions