Имеем: #include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define BUFFSIZE 32
void Die(char *mess) { perror(mess); exit(1); }
int main(int argc, char *argv[]) {
int sock;
struct sockaddr_in echoserver;
char buffer[BUFFSIZE];
unsigned int echolen;
int received = 0;
char sentdstring[250];
if (argc != 5) {
fprintf(stderr, "USAGE: ./a.out <server_ip> <key> <ip> <proto>\n");
exit(1);
}
/* Create the TCP socket */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
Die("Failed to create socket");
}
/* Construct the server sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
echoserver.sin_family = AF_INET; /* Internet/IP */
echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */
echoserver.sin_port = htons(atoi("10000")); /* server port */
/* Establish connection */
if (connect(sock,
(struct sockaddr *) &echoserver,
sizeof(echoserver)) < 0) {
Die("Failed to connect with server");
}
/* Send the word to the server */
//string sentdstring;
//string sentdstring("key "+argv[2]+" intruder "+argv[3]+" proto "+argv[4]);
echolen = strlen(argv[2]);
if (send(sock, argv[2], echolen, 0) != echolen) {
Die("Mismatch in number of sent bytes");
}
echolen = strlen(argv[3]);
if (send(sock, argv[3], echolen, 0) != echolen) {
Die("Mismatch in number of sent bytes");
}
echolen = strlen(argv[4]);
if (send(sock, argv[4], echolen, 0) != echolen) {
Die("Mismatch in number of sent bytes");
}
/* Receive the word back from the server */
fprintf(stdout, "Status: ");
while (received < echolen) {
int bytes = 0;
if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) {
Die("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0'; /* Assure null terminated string */
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);
exit(0);
}
вопрос... никак не могу понять (в силу того, что забыл) как мне всё запихнуть в одну переменную и отправить её, переменная sentdstring("key "+argv[2]+" intruder "+argv[3]+" proto "+argv[4]);
Дальше бы просто
echolen = strlen(sentdstring);
if (send(sock, sentdstring, echolen, 0) != echolen) {
Die("Mismatch in number of sent bytes");
}
подскажите пожалуйста, а то чего то торможу...