Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active October 2, 2018 18:59
Show Gist options
  • Save erfg12/edab8db2d07bbc1ef8b77aa8bfcde454 to your computer and use it in GitHub Desktop.
Save erfg12/edab8db2d07bbc1ef8b77aa8bfcde454 to your computer and use it in GitHub Desktop.
TCP client C
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#ifdef _WIN32
#include <winsock2.h>
#include "windows.h"
#endif
#ifdef __linux__
#include <pthread.h>
#include <sys/socket.h>
#endif
#pragma warning(disable:4996)
#pragma comment(lib, "Ws2_32.lib")
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
char buf[8192]; // our read buffer from socket stream
char storage; // information to display
SOCKET SendingSocket;
SOCKADDR_IN ServerAddr, ThisSenderInfo;
struct sockaddr_in myaddr;
WSADATA wsaData;
int RetCode;
void TCP_connect(char ip[255], int port) {
// start winsock
WSAStartup(0x101, &wsaData);
//printf("Client: Winsock DLL status is %s.\n", wsaData.szSystemStatus);
// create socket
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SendingSocket == INVALID_SOCKET)
{
printf("Client: socket() failed! Error code: %ld\n", WSAGetLastError());
WSACleanup();
}
//else
// printf("Client: socket() is OK!\n");
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(port);
ServerAddr.sin_addr.s_addr = inet_addr(ip);
// create connection
RetCode = connect(SendingSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));
if (RetCode != 0)
{
printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError());
closesocket(SendingSocket);
WSACleanup();
}
else
{
printf("Client connected. Ready for send/receive data.\n");
}
}
void sendPkt(char *sndBuf) {
int res = send(SendingSocket, sndBuf, (int)strlen(sndBuf) + 1, 0);
//printf("Bytes Sent: %d\n", res);
}
void readWin32Stream() {
while (1) {
int result;
do {
result = recv(SendingSocket, buf, 512, 0);
if (result > 0) {
//printf("[DEBUG] raw data received: %s\n", buf);
int bal = result;
int ptr = strlen(buf); // count chars before first occurrence of null char
int prevNull = 0;
if (ptr > 0) {
//printf("[DEBUG] null found! bal=%d ptr=%d result=%d\n", bal, ptr, result);
int beforeNull = ptr;
while (bal >= beforeNull)
{
//printf("[DEBUG] bal=%d beforeNull=%d prevNull=%d\n", bal, beforeNull, prevNull);
char* storage = malloc(8192);
strncpy(storage, buf + prevNull, beforeNull + 1); //create our temporary storage variable for a section of our socket streaam
int tmpStore = beforeNull + 1;
//printf("[DEBUG] storage size=%d, storage=%s\n",sizeof(storage), storage);
// find another null char
int scndPtr = strlen(buf + beforeNull + 1);
if (scndPtr > 0) {
//printf("[DEBUG] Found another null char in the buffer\n");
beforeNull = scndPtr; //another null char in stream?
//printf("[DEBUG] New beforeNull = %d\n", scndPtr);
}
bal = (bal - tmpStore); //count remaining bytes
prevNull = tmpStore;
printf("received: %s\n", storage);
memset(storage, 0, sizeof storage); //empty storage
if (bal > 0)
{
//printf("[DEBUG] balance remaining: %d\n", bal);
strncpy(storage, buf + beforeNull + bal, 0); //store remaining bytes
}
if (beforeNull <= 0) {
memset(buf, 0, sizeof buf); //clear buffer
break;
}
}
}
}
} while (result > 0);
}
}
void readLinuxStream() {
while (1) {
//code goes here
}
}
#ifdef _WIN32
DWORD WINAPI readThread(LPVOID lpParameter) {
//printf("listening thread is running\n");
readWin32Stream();
return 0;
}
#endif
#ifdef __linux__
void *readThread(void *sntCmd)
{
readLinuxStream();
}
#endif
int main(void)
{
// make connection
TCP_connect("127.0.0.1", 13000);
// send test
sendPkt("lm|");
// read buffer test
readStream();
/*#ifdef _WIN32
DWORD threadID;
HANDLE thread = CreateThread(NULL, 0, readThread, NULL, 0, &threadID);
CloseHandle(thread);
#endif
#ifdef __linux__
pthread_t tid;
pthread_create(&tid, NULL, &readThread, &cmd);
pthread_detach(&tid);
#endif*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment