Last active
October 4, 2019 17:27
-
-
Save prydin/2f7732e14105ed79993c76c09581ad49 to your computer and use it in GitHub Desktop.
Sample message sender in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define NUM_MSG 10 /* Change this to set how many messages we process with a single call to send() */ | |
int main(int argc, char const *argv[]) | |
{ | |
int sock = 0, valread; | |
struct sockaddr_in serv_addr; | |
char *one_message = "|slca|trafficgenerator3|hyperlvs76|31|CalPublisherTest1.0|unset|67c2da756f9f509b|T06:49:36.00 ERROR root-0 0 10 corr_id_=002a4eae4b97f&log_id_=012a50414b97e&m_rc=Retry&m_cfgset_name=m_cfgset_name&m_credit_issuer=m_credit_issuer&m_txn_timestamp=1564249775&m_end_time=1564382975&m_norm_cvv_resp_code=m_norm_cvv_resp_code&m_sndr_price_category=m_sndr_price_category&m_cfgset_service_name=m_cfgset_service_name&m_tokenized_value_type=m_tokenized_value_type&m_cfgset_name=m_cfgset_name&m_user_acct_type=m_user_acct_type&m_conversion_type=m_conversion_type&m_cfgset_version=m_cfgset_version&m_ext_wallet_providers=m_ext_wallet_providers&m_start_time=1564418975&m_incentive_funding_type=m_incentive_funding_type&m_extn_partner_incentive_type=m_extn_partner_incentive_type&m_is_millennium_eligible=m_is_millennium_eligible&m_dest_curr=m_dest_curr&m_fee_type=m_fee_type&m_source_curr=m_source_curr&m_bank_txn_type=m_bank_txn_type&m_compliance_decision=m_compliance_decision&m_bank_routing_entity=m_bank_routing_entity&m_rcpnt_price_category=m_rcpnt_price_category&m_txn_acquirer_country=m_txn_acquirer_country&m_partner_client_id=m_partner_client_id&m_api_name=m_api_name&m_proc_endpoint_uri=m_proc_endpoint_uri&m_balance_type=m_balance_type&call-duration=4711&m_bank_proc=m_bank_proc\n"; | |
int l = strlen(one_message); | |
char *message = malloc(l * NUM_MSG); | |
char *p = message; | |
for(int i = 0; i < NUM_MSG; ++i) { | |
memcpy(p, one_message,l); | |
p += l; | |
} | |
/* Notice the TCP_NODELAY flag. This *may* result in faster and more efficient handling of | |
large payloads. It may also reduce (but not eliminate) the number of partial messages. */ | |
if ((sock = socket(AF_INET, SOCK_STREAM, TCP_NODELAY)) < 0) | |
{ | |
printf("Socket creation error\n"); | |
return -1; | |
} | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(atoi(argv[2])); | |
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) | |
{ | |
printf("Invalid address/ Address not supported: %s\n", argv[1]); | |
return -1; | |
} | |
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) | |
{ | |
printf("Connection Failed \n"); | |
return -1; | |
} | |
for(;;) { | |
int size = l * NUM_MSG; | |
char *m = message; | |
for(;;) { | |
int n = send(sock , m , size , 0); | |
if(n < 0) { | |
printf("Send Failed\n"); | |
return -1; | |
} | |
/* If we could sen the entire message, we're done. */ | |
if(size == n) { | |
break; | |
} | |
/* Only part of the message sent. Advance buffer pointer by n steps and reduce size of remaining | |
message by n. Then try again. */ | |
m += n; | |
size -= n; | |
printf("%d bytes remain to be sent, and that's OK!\n", size); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment