|
|
Comments and Discussions
|
|
 |

|
Please send me the zipped project file. I have problems compiling your code correctly.
Thank you and best wishes for you and your family.
Victor
vnla@hotmail.com
|
|
|
|

|
This is really nice piece of work. I most enjoyed the simple explanations and direct example.
|
|
|
|

|
Hi Nishant Sivakumar,
Can you send me the project file to my email address at byronli66@qq.com?
Thanks.
best Regards
|
|
|
|

|
hi. nish
i have to write a program in which the client send the http request to server for receive the file .server provide the client a http url and on hitting the url the client data send to server.
i am using window environment + visual st. for writing program in c lang.
i have make client-server program using winsock api. which work fine for sending data but in this case the send has to listen for client and then client execute.
But i want that client send req. to server url and file transfer.how it can be if we send file from client side how server received it from it end. help
|
|
|
|

|
Would it be possible for you to send me the zipped project file? I have problem compiling your code correctly and maybe i could learn more by having the actual project file.
Thanks you and wishes you and your family the best.
fabmkk@gmail.com
|
|
|
|

|
Hi Nishant Sivakumar,
Can you send me the project file to my email address at ikurtoglu@yahoo.com?
Thanks.
Regards,
Ismail
|
|
|
|

|
sorry can u send to sheran_silva@hotmail.com
that would be much appreciated
sheran
|
|
|
|

|
hey can u send me the file
thx
|
|
|
|

|
Hi Nishant Sivakumar,
Can you send me the project file to my email address at cafukarfoo@yahoo.com?
Thanks.
Regards,
Chong kar foo
|
|
|
|

|
Dear Nis!
Excellent Article!
Thanks!
|
|
|
|

|
dear Mr.Nishant,
i need the source code for this project.i am not able to compile it.can you please send me the zip file to my email id, aarthi.krishnan@yahoo.com
thank you
aarthi krishnan
|
|
|
|

|
thanks for this code and can you send zip file of it.
|
|
|
|

|
hello,i was wondering if you could help me in a beginners and i would like to learn socket programming was wondering if you could send me the zip file for this code thank you my email is
"hwzlake@163.com"
thanks
|
|
|
|

|
Compiling...
server.cpp
h:\vc++ exercies in vs 6.0\server\server.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/server.pch': No such file or directory
Error executing cl.exe.
server.obj - 1 error(s), 0 warning(s)
How to solve....
i have choose radio button as MFC suport, Add this 2 code to server.cpp file.
also add ws2_32.lib to setting-->link
server.cpp
---------------------------------------------------------------------------------------
// server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "server.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
#include<windows.h>
#include <afxwin.h>
using namespace std;
iint _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
cout << "Press ESCAPE to terminate program\r\n";
AfxBeginThread(ServerThread,0);
while(_getch()!=27);
return nRetCode;
}
UINT ServerThread(LPVOID pParam)
{
cout << "Starting up TCP server\r\n";
//A SOCKET is simply a typedef for an unsigned int.
//In Unix, socket handles were just about same as file
//handles which were again unsigned ints.
//Since this cannot be entirely true under Windows
//a new data type called SOCKET was defined.
SOCKET server;
//WSADATA is a struct that is filled up by the call
//to WSAStartup
WSADATA wsaData;
//The sockaddr_in specifies the address of the socket
//for TCP/IP sockets. Other protocols use similar structures.
sockaddr_in local;
//WSAStartup initializes the program for calling WinSock.
//The first parameter specifies the highest version of the
//WinSock specification, the program is allowed to use.
int wsaret=WSAStartup(0x101,&wsaData);
//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
return 0;
}
//Now we populate the sockaddr_in structure
local.sin_family=AF_INET; //Address family
local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
local.sin_port=htons((u_short)20248); //port to use
//the socket function creates our SOCKET
server=socket(AF_INET,SOCK_STREAM,0);
//If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
return 0;
}
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{
return 0;
}
//listen instructs the socket to listen for incoming
//connections from clients. The second arg is the backlog
if(listen(server,10)!=0)
{
return 0;
}
//we will need variables to hold the client socket.
//thus we declare them here.
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);
while(true)//we are looping endlessly
{
char temp[512];
//accept() will accept an incoming
//client connection
client=accept(server,
(struct sockaddr*)&from,&fromlen);
sprintf(temp,"Your IP is %s\r\n",inet_ntoa(from.sin_addr));
//we simply send this string to the client
send(client,temp,strlen(temp),0);
cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";
//close the client socket
closesocket(client);
}
//closesocket() closes the socket and releases the socket descriptor
closesocket(server);
//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
WSACleanup();
return 0;
}
|
|
|
|

|
Hi Nishant,
I am having just one month of experience in network programming. I want to trace TCP packets using winsock. I could right the code for that as below. It worked on XP 32 bit but not on Vista Home 32 bit machine.
SOCKET sniffSocket;
if((sniffSocket= socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)
{
return 0;
}
struct sockaddr_in src;
memset(&src, 0, sizeof(src));
src.sin_addr.S_un.S_addr = inet_addr (pAdapterInfo->IpAddressList.IpAddress.String);
src.sin_family = AF_INET;
src.sin_port = 0;
if (bind(sniffSocket,(struct sockaddr *)&src, sizeof(src)) == SOCKET_ERROR)
{
return 0;
}
int j=1;
if (WSAIoctl(sniffSocket, SIO_RCVALL, &j, sizeof(j), 0, 0, &in, 0, 0) == SOCKET_ERROR)
{
return 0;
}
char *pkt_data = (char *)malloc(65536); int res;
do
{
res = recvfrom(sniffSocket, pkt_data, 65536, 0, 0, 0); ...
...
}
while (res > 0);
...
...
The above code is working fine on XP. I could trace TCP/IP packets (in variable pkt_data) this way. But when I run the same code on Vista, I am able to trace just UDP/IP packets. Can you tell me why is it not working on Vista?
I hope you can resolve my problem.
My second query: When we type a web-site address on a browser, we don't tell which adapter to connect to. Browser automatically opens adapter that is connected to internet. How it does that? I mean how can I detect which adapter is being used for internet connection?
Thanks in Advance
Regards
Aseem
modified on Wednesday, December 23, 2009 1:48 AM
|
|
|
|
|

|
Hi Nishant !!
Thnx for the excellent Post , however can you send the project zip file to my email id
yithape@gmail.com
|
|
|
|

|
Dear Mr.Nishant,
can you plz send me the zip file for this project? My email is lwang@u-systems.com. Thank you!
|
|
|
|

|
Dear Mr.Nishant,
can you plz send me the zip file for this project? i have tried few times but not succeed to compile it. my email is jd_boon@hotmail.com. Thx
|
|
|
|

|
hello was wondering if you could help me im a beginners and i would like to learn socket programming was wonmdering if you could send me the zip file for this code thank you my email is
"siu07tk@rdg.ac.uk"
thanks
|
|
|
|

|
Dear Mr.Nishat:
I am a new learner,i have several probems that i cannot sovle itmyself,can you send me the zip file please?my e-mail:zhaozhihong1008@gmail.com
Best wishes
zhaozhihong
|
|
|
|
|

|
Dear Mr.Nishat:
I am a new learner,i have several probems that i cannot sovle itmyself,can you send me the zip file please?my e-mail:lovelr1209@163.com
Best wishes
lovelr
|
|
|
|

|
Dear Mr.Nishat
Can you please send me the zip file for this project...My e-mail is: justin73125@yahoo.com.tw
Thank you.
Best Regards,
Justin
|
|
|
|

|
hey everyone, I am a rookie in network programming. Does this code compile succesfully on VS2005????
I am having troubles with windows.h
|
|
|
|

|
Dear Mr.Nishat
Can you please send me the zip file for this project...My e-mail is: 7ozayen@gmail.com
Thank you.
Best Regards,
Ahmed Hozayen,
|
|
|
|

|
I need it too
pacckal@mail.ru thx!
|
|
|
|

|
From the article:
Open the file stdafx.h and add the following line :- #include <winsock2.h>
Also #include conio.h and iostream just after winsock2.h
Why should I, a novice at TCP/IP programming, be required to edit any of the standard include files? Is there no way to make the code work without editing these standard delivered files?
Thanks for your time
|
|
|
|

|
stdafx.h is not a standard C++ header file. It's just the default header file Visual Studio generates for you in which you can put all the common header files. The difference is that this header file is pre-compiled by VC++ (unless the compiler setting is set not to use precompiled headers). While I don't remember the exact compiler switch, you can get it to use a different name (than stdafx) if you want to, but this is the default name.
|
|
|
|

|
How i can limit the access at the server only from localhost?
tnx
|
|
|
|
|

|
The only need for MFC is AfxBeginThread, and _beginthreadex can be used instead of it, as in:
hThread = (HANDLE)_beginthreadex(NULL, 0, ServerThread, 0, 0, &ThreadId);
if (hThread == 0) {
ErrorNumber = errno;
DOSErrorNumber = _doserrno;
return 0;
}
Then the few variables used there are:
unsigned ThreadId;
HANDLE hThread;
int ErrorNumber, DOSErrorNumber;
And ServerThread could be:
unsigned __stdcall ServerThread(void *pArgument)
Then instead of #include afxwin.h we need to #include windows.h and #include process.h.
I might have overlooked one or two details, but my modifications worked for me using _beginthreadex instead of AfxBeginThread.
-- modified at 3:45 Friday 27th July, 2007
|
|
|
|

|
i even changed it to use boost thread.
as such killing the thread is kind of a hassle.
this will start the thread
define
the variable
boost::thread *m_pThread;
and start the thread as
m_pThread = new boost::thread(boost::bind(&jcTCP::ServerThread, this));
this worked for me. i like the boost thread better than the windows thread because of the portability issue
|
|
|
|

|
Excellent code nishant,but i only get the msg"starting up TCP server.I do not get the server and client IP address.Also I have 2 computers and I wish to connect the other as client.Can I connect these two computers using telnet?if yes,how?Kindly help me out in the step by step procedure of connection after running the code.
Urgent!!!!!!
Best Regards!
|
|
|
|

|
I am not the author of this article, but I am nearly certain that it is not possible to connect two computers using this code. This is only the server portion. The client is telnet. This is a very simple sample. Have you looked at the subsequent articles?
To rest this program, use telnet in the manner I showed in answers below.
|
|
|
|

|
Thank you Sam.But I disagree with you,this code alone along with the subsequent code for the client can be used to connect two computers,albeit you need to introduce some changes.Infact I would suggest all the coders to introduce some simple msgs like send(on the client side),recv(provided the client is sending sumthing,eg.a simple text msg,a file,etc)on the server side in this code.Same needs to be done on the client side as well.This will enable the server and client to communicate very easily.
Best Regards!
|
|
|
|

|
reena vade wrote: albeit you need to introduce some changes
Exactly. That is the essential point. If the code is used as-is then it does not connect two computers. That is all I meant to say. It is a start but it needs more, even if it is just a little bit more that it needs.
|
|
|
|

|
Because if I use number port 80 ( htons(80) ) I have an error on bind instruction?
If I use another number port I have not error on bind instruction?
Thanks
|
|
|
|

|
Because you have used the port 80 .the web site's defaulted port is 80!
|
|
|
|

|
hi,
after seeing the whole site i felt your athe only jame who can help me
do you know some thing about this
we have to create some thing like this
we have to split a file to be secured and it is split into required no of parts and each part is encypted and sent to various systems in the network so we use socket program to
send files and problem here is we have different clients and
so we have tos send perticular files to each client here you have given the host address in sockSrvr how do we get the address we want how do i do it.
Another thing is when ever a person from the client side wants to open the complete file all the files from the network which were sned to each client from srever,have to gathered and decrypted and joined and open the full file when we give a command from client how do i do this this.
is this posible i can give you a sample code but i feel its not correct can you just give me a code of this case
like when the client wants to open the full file
he has to passon the some protocal to server and server gets the information of all the file from the notepad we manintain and then call all the clients to send the files and then server joins all the files after decryption of each and sends the file to requested client
i think this si really an exam like thing bt i felt happy after seeing your work if you can help me getiing file from clients to server for joinging
i have splitter and joinner i.e. ffsj.exe search in internet you will get it
i used that to split and send.exe your code to send files each split file after encrypting it
problem is how do i get this getting file from all clients and join them after decryption of each file
we can open the splitter ffsj.exe using system("c:\\ffsj.exe");
and others also same way
this is not pain its your greates achive ment i am asking you hel me get file on request thats it and reset i will do it.
|
|
|
|

|
Superb article
|
|
|
|

|
First I want to say "Thank you"
It is my first programing of winsock,and so luckly I found your article,It is useful for me to study winsock.
For more, It is time for me to improve my English.
|
|
|
|

|
Hi there,
I bought a D-link IP camera, I though it should display the video on my own GUI (in C++ or Java). But the IP camera is built for displaying the video on web browser. How should I bring the IP camera video display into my own GUI interface. if anyone have the answer for this question???
regards
Baalan
|
|
|
|

|
Hi Baalan,
I have a IP-Cam of "MOBOTIX", using the SDK of the Camera I can easy put Video in my own applicaton(e.g. VB application ). I think you should find the SDK for your Kamera, maybe refer to the support of D-Link.
good luck!
|
|
|
|
|

|
I can't read a number of the messages. Some I can. What do I do ?
|
|
|
|

|
Hi, nice article - one thing though - you're listening on 0.0.0.0 (INADDR_ANY). When trying to listen on localhost or a specific interface/ip, I had to wrap the IP in a htonl call (probably due to the little endian nature of x86) like this: local.sin_addr.s_addr=htonl(INADDR_LOOPBACK); Maybe it would be a good idea to wrap the INADDR_ANY in a htonl call (even if it doesn't do anything for INADDR_ANY) just to help people trying to bind to something else. Thanks.
|
|
|
|

|
Hi Nishant,
Was tring to compile your code, and I received this error:
------ Build started: Project: Socket_2, Configuration: Debug Win32 ------
Compiling...
stdafx.cpp
c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxv_w32.h(16) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include
Build log was saved at "file://d:\Visual Studio Training\Socket_2\Socket_2\Debug\BuildLog.htm"
Socket_2 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any idea of how to solve this??!!
Thanks!
|
|
|
|

|
You must not include the header file:
#include <windows.h>
in your project (keep in mind that this has MFC support).
You already have
#include <afxwin.h>
and that is all you need.
I am in love with VC++
|
|
|
|

|
I found the same problem when using Visual Studio.NET 2003 (version 7), when I added my includes at the top of stdafx.h. I got a successful build by moving the includes specifed in the article to after those automaticaly generated by Visual Studio in stdafx.h.
Dave Turner
Sydney
Australia
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
An introduction to WinSock programming. A simple TCP server is explained.
| Type | Article |
| Licence | |
| First Posted | 25 Feb 2002 |
| Views | 626,008 |
| Bookmarked | 243 times |
|
|