Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello everyone.. I'm new in C++ and here is the code for basic client and server.. (I am trying to code a chat program).

Server :

C++
#include "stdafx.h"
#include<winsock.h>
#include<string.h>
#include<stdlib.h>
#include<iostream.h>
#pragma comment(lib, "wsock32.lib")

int  main()
{
	WSADATA ws;
	int nret;
	WSAStartup (0x0101,&ws);
	
	SOCKET commsocket;
	char sendbuffer[256]=" Recieved! ";
	char recvbuffer[256];
	SOCKET listeningSocket;
	listeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	if(listeningSocket==INVALID_SOCKET)
	{
		cout<<"Could not creat listening socket";
		exit(1);
	}
	
	SOCKADDR_IN serverInfo;
	serverInfo.sin_family=AF_INET;
	serverInfo.sin_addr.s_addr=inet_addr("127.0.0.1");
	serverInfo.sin_port=htons(10000);
 	nret=bind(listeningSocket,(SOCKADDR *)&serverInfo,sizeof(struct sockaddr));
	if(nret==SOCKET_ERROR)
	{
		cout<<"Could not bind listening socket";
		exit(1);
	}

	nret=listen(listeningSocket,10);
	if(nret==SOCKET_ERROR)
	{
		cout<<"Could not listen";
		exit(1);
	}
	commsocket=accept(listeningSocket,NULL,NULL);
	if(commsocket==INVALID_SOCKET)
	{
		cout<<"Could not creat listening socket";
		exit(1);
	}
	while(1)
	{
	nret=recv (commsocket,recvbuffer,255,0);
	recvbuffer[nret]=0;
	if (nret==SOCKET_ERROR)
	{
		cout<<"could not receive message from client";
		exit(1);
	}
	else
	{
		cout<<recvbuffer;
		cout<<("cool");
		nret=send (commsocket,sendbuffer,255,0);
		if (nret==SOCKET_ERROR)
		{
			cout<<"could not send message to client";
				exit(1);
		}
	}
	}

 closesocket (commsocket);
 closesocket (listeningSocket);
 WSACleanup();
 
 return 0;
}

Client:
C++
#include "stdafx.h"
#include<winsock.h>
#include<string.h>
#include<stdlib.h>
#include<iostream.h>

#pragma comment(lib, "wsock32.lib")
int main()
{
 	WSADATA ws;
	int nret;
	WSAStartup (0x0101,&ws);
	char sendbuffer[256]="From Client: Hi ....";
	char recvbuffer[256];
	SOCKET commsocket;
	commsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(commsocket==INVALID_SOCKET)
	{
		cout<<"Could not creat comm. socket";
		exit(1);
	}

	SOCKADDR_IN serverInfo;
	serverInfo.sin_family=AF_INET;
	serverInfo.sin_addr.s_addr=inet_addr("127.0.0.1");
	serverInfo.sin_port=htons(10000);
	nret=connect(commsocket,(LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
	if(nret==SOCKET_ERROR)
	{
		cout<<"Could not connect to server";
		exit(1);
	}
	nret=send(commsocket,sendbuffer,strlen(sendbuffer),0);
	if(nret==SOCKET_ERROR)
	{
			cout<<"Could not send bytes to server";
			exit(1);
	}
	nret=recv(commsocket,recvbuffer,32,0);
	if(nret==SOCKET_ERROR)
	{
			cout<<"Could not connect to server";
			exit(1);
	}

	else
		cout<<recvbuffer;
	closesocket (commsocket);
	
	return 0;
}

After successfully execute the server and client, I am unable to display the message from client (sent by client to server) and message from server(sent by server)..!!
Posted
Comments
nv3 21-Jun-12 8:20am    
So, what happens instead? Do client and server hang, or do they display an error message, or what? Have you used a debugger and stepped through the server or client?
sourcecode2012 21-Jun-12 10:05am    
Yes...everything is executing successfully... If I telnet localhost on port 10000 then itshows the message from server. but If I use client then there is no output...also I tried to write the received string by server in a text file which means server is successfully receiving the string from client.
Albert Holguin 21-Jun-12 9:30am    
You have to be more specific with your problem statement, "not being able to display the message" is too vague. Did you receive a packet? Does the packet have the binary buffer loaded? Do you not know how to cast this data appropriately? Did the tcp/ip connection even succeed?

1 solution

I see two things in your code:

(a) You receive only with a buffer length of 32. These will be filled from the ASCII in the server's reply (which is 256 bytes long). The storage behind these 32 bytes is undefined. Perhaps you want to receive 256 bytes there and limit the server's response to strlen (sendbuffer) +1.

(b) The cout statement after you receive something does not contain a \n. So your output might still be in the stdio buffer when you terminate the program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900