Solved

Sample codes for Socket programming

Posted on 2004-08-29
14
476 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug Angular JS in Visual Studio 2013 2 170
computer science syllabus 3 79
White board coding practice 3 86
designing in object programming 12 75
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

815 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

7 Experts available now in Live!

Get 1:1 Help Now