Solved

Sample codes for Socket programming

Posted on 2004-08-29
14
473 Views
Last Modified: 2013-12-14
Hi, I am currently developing a application that requires socket programming. I wonder if anyone out there could share with me some webby that provides socket programming codes for C++. I'm using visual studio 6.0 for my server application, and embedded visual C++ for my client side. I am totally new to socket programming thus I need basic explaination. Thank you.
0
Comment
Question by:mwcmp
  • 3
  • 2
  • 2
  • +4
14 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 74 total points
ID: 11925972
Hi mwcmp,
A chat application is always the simplest way to start learning socket programming, I think.
You will find many examples of socket programming with Visual C++ at codeproject.com. There is a special beginner's section:
http://www.codeproject.com/internet/


Good luck,
Jaime.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 71 total points
ID: 11926367
If you don't want to use MFC, take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/finished_server_and_client_code.asp ("Complete Server and Client Code")
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 11926429
0
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 71 total points
ID: 11926440
oh, I forgot also check gametutorials.com they have a networking section in the tutorials ideal for beginners

http://gametutorials.com/Tutorials/Networking/Networking_Pg1.htm
0
 

Assisted Solution

by:sumn79
sumn79 earned 71 total points
ID: 11928247
try this: this sample just make a HTTP post to a php

#include <windows.h>
#include <winsock.h>

void main()
{
      if (!InitWinSock()) return;
      HTTPPost("192.168.1.100",80,"/testsocket.php",NULL,0,NULL)
      WSACleanup();
}

BOOL InitWinSock()
{
      WSADATA wsaData;
      int iResult=WSAStartup(MAKEWORD(2,2),&wsaData);
      if (iResult!=NO_ERROR) return FALSE;

      return TRUE;
}

BOOL HTTPPost(const char *addr,int port,char *urlMsg,char *data,int dataLen,char *retStr)
{
      if (urlMsg==NULL) return FALSE;
      if (data==NULL&&dataLen>0) return FALSE;
      
      SOCKET m_socket;
      m_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
      BOOL isSent=FALSE;
      if (m_socket!=INVALID_SOCKET)
      {
            sockaddr_in clientService;

            clientService.sin_family=AF_INET;
            clientService.sin_addr.s_addr=inet_addr(addr);
            clientService.sin_port = htons(port);
            if (connect(m_socket,(SOCKADDR*)&clientService,sizeof(clientService))!=SOCKET_ERROR)
            {
                  char sendbuf[1024];
                  wsprintf(sendbuf,"%s HTTP/1.0\r\n",urlMsg);
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Content-type: application/x-www-form-urlencoded\r\n");
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Content-length: %d\r\n",dataLen);
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Connection: close\r\n\r\n");
                  send(m_socket,sendbuf,strlen(sendbuf),0);

                  if (data!=NULL)
                  {
                        send(m_socket,data,dataLen,0);
                  }
                  isSent=TRUE;
                  
                  int bytesRecv=SOCKET_ERROR;
                  char recvbuf[1024];
                  int accSize=0;
                  while( bytesRecv == SOCKET_ERROR )
                  {
                        bytesRecv = recv(m_socket,recvbuf,1024,0);
                        accSize+=bytesRecv;
                        if (bytesRecv==0||bytesRecv==WSAECONNRESET)
                        {
                              break;
                        }
                        printf("Bytes Recv: %ld\n",bytesRecv);
                  }
                  *(recvbuf+accSize)='\0';
                  if (accSize>0&&retStr!=NULL)
                  {
                        lstrcpy(retStr,recvbuf);
                  }

            }
            
            closesocket(m_socket);
      }      
      return isSent;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Assisted Solution

by:carribus
carribus earned 71 total points
ID: 11928839
Hi there,

there are various ways to tackle socket programming, but to learn it, I can only point you in the direction that I went.
Have a look at this document called : Beej's Guide to Network Programming.

http://www.ecst.csuchico.edu/~beej/guide/net/

It is written for people who have NEVER done network development before, and therefore is easy to follow and understand.

Enjoy!
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 71 total points
ID: 11936318
Try the WinSock Programmer's FAQ: http://tangentsoft.net/wskfaq/

0
 
LVL 4

Expert Comment

by:havman56
ID: 11953114
Lot of developers used my socket code and got benefited

u can go and find it in my web site

http://micro_mangal.tripod.com 

it is in free code section

0
 
LVL 4

Assisted Solution

by:havman56
havman56 earned 71 total points
ID: 11953149
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12310444
I think i made comments, so I should get at least a share of the points
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12328005
All we have provided samples for socket programming.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now