I'm working on a code that enables the user to input a string , compare it to some text in a file then send the approriate data ( from another file) using serial communication or TCP/IP. I ahve implemented the serial communication part but the TCP/IP..I've looked around but have not been able tofigure out how to start working in that direction. I'm working on windows ans I'm using visual studio. If anyone could please show me some direction regarding this..Kindly help out. Thanks.

To start with file transfer using TCP/IP refer any guide or even text book that describes "Socket Communication Programming in C"...u can also google it for help...
Understand the concept, give it a try, then post ur code for any problems that u face.
We r ready to try solving, once u give it a try.
As for starter, primarily to construct ur code for client-server communication using sockets in C, u will require the following libraries :
1. socket() - create a new socket and return its descriptor
2. bind() - associate a socket with a port and address
3. listen() - establish queue for connection requests
4. accept() - accept a connection request
5. connect() - initiate a connection to a remote host
6. recv() - receive data from a socket descriptor
7. send() - send data to a socket descriptor
8. close() - close of a socket descriptor

Thanks for the reply..this is the clarification I was looking for..just the basic pointers..will work around the program and post if any stumbling blocks..thanks again..

U can check out this link for very basic examples on TCP/IP sockets...
Also check the references mentioned there. I personally would prefer the "Beej's guide to Network Programming" for starters.

Note : Plz mark the thread as solved.
Cheers,

Here is some sample code:

Samples C++ Code to do SMTP From a Win32 Console
#include <iostream.h>
#include <winsock2.h>
#include <windows.h>
//#include <string>
#include <stdio.h>
#include <string.h>

//using namespace std;




#define MAX_LENGTH 1024
#pragma argsused

char *fgets(char *line, int maxline, FILE *fp);
int fputs(char *line, FILE *fp);
int getline(char *line, int max);

void main()
{
int s_len, r_len;
int skt_Smtp;
int success;
struct sockaddr_in st_Sockaddr;
char recv_Buf[MAX_LENGTH];
char send_Buf[MAX_LENGTH];

char abuf[MAX_LENGTH] = "";;
char bbuf[MAX_LENGTH] = "";;
//using namespace std;
//string my_string;


char text[1024] = "";



//Initialize Sockets
WSADATA wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);


skt_Smtp = socket(AF_INET,SOCK_STREAM,0);
if (skt_Smtp < 0)
{
cout<< "Error Creating Socket"<<endl;
return;
}
else
{
st_Sockaddr.sin_family = AF_INET;
st_Sockaddr.sin_port = htons(25);

//Get the IP address and initialize the structure
st_Sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

success = connect(skt_Smtp,(struct sockaddr *) &st_Sockaddr,sizeof(st_Sockaddr));

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Say Hello to the domain
strcpy(send_Buf,"HELO someoneatsomewhere.net\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Send from address

strcpy(send_Buf , "MAIL FROM: <someone@gmail.com>\r\n");


s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;

//Send RCPT address
//strcpy(send_Buf,"RCPT TO: <someone@gmail.com>\r\n");
cout << "RCPT TO:";
//strcpy(text,"RCPT TO:");
strcat(bbuf,"RCPT TO:<");

getline(text,255);

text[strlen(text)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */

strcat(bbuf, text );

//cout << text;

strcat(bbuf,">");


strcpy(send_Buf,bbuf);

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);


cout<< send_Buf<<endl;

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);

recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;

// Send DATA
strcpy(send_Buf,"DATA\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;


// Send DATA

//strcpy(send_Buf,"Subject:new subject\r\n\r\n\r\n");
cout << "Enter Subject:";
strcpy(text,"Subject:");
getline(&text[8],255);
int t;
//t=sizeof(text);
t = strlen(text);
strcpy(&text[t],"\r\n\r\n\r\n");

//** printf("\nSubject:%s",text);

strcpy(send_Buf,text);

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

//Send body
cout << "Enter Message:";
getline(text,1024);
int q;
q=sizeof(text);
strcpy(&text[q],"\r\n.\r\n");

//** printf("\nText:%s",text);

strcpy(send_Buf, text );

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n.\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Send QUIT
strcpy(send_Buf,"QUIT.\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

closesocket(skt_Smtp);

}

WSACleanup();

}



/* fgets: get at most n chars from iop */
char *fgets(char *s, int n, FILE *iop)
{
register int c;
register char *cs;

cs = s;
while (--n > 0 && (c = getc(iop)) != EOF)
if ((*cs++ = c) == '\n')
break;
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}

/* fputs: put string s on file iop */
int fputs(char *s, FILE *iop)
{
int c;

while (c = *s++)
putc(c, iop);
return ferror(iop) ? EOF : 0;
}
int getline(char *line, int max)
{
if (fgets(line, max, stdin) == NULL)
return 0;
else
return strlen(line);
}

Edited 6 Years Ago by technology: n/a

This question has already been answered. Start a new discussion instead.