Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a beginner to socket programming. I'm using eclipse ide in C++ language. I tried a simple client-server program and it gives me an error in read() method of the server program.

This is my server program:
C++
void Server2::createSocket(){
	#ifdef WIN32
	// Initialize Winsock
	int iResult;
	WSADATA wsaData;
	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (iResult != 0) {
		std::cout << "WSAStartup failed: " << iResult << std::endl;

	}
	#endif

	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	if(sockfd < 0){
		int err = WSAGetLastError();
		cout << "Create socket failed with error " << err << endl;
		exit(EXIT_FAILURE);
	}

}

void Server2::clearAdrressStructure(){

	bzero((char*)&serv_addr, sizeof(serv_addr));

}

void Server2::bindSocket(){
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);

	if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
		int err = WSAGetLastError();
		cout << "Bind failed with error " << err << endl;
		exit(EXIT_FAILURE);
	}

}

void Server2::listenConnection(){

	int a = listen(sockfd, 5);

	if(a < 0){
		int err = WSAGetLastError();
		cout << "Listen failed with error " << err << endl;
		exit(EXIT_FAILURE);
	}

}

void Server2::acceptConnection(){
	clilen = sizeof(clien_addr);

	newsockfd = accept(sockfd, (struct sockaddr *)&clien_addr, &clilen);

	if(newsockfd < 0){
		int err = WSAGetLastError();
		cout << "Accept failed with error " << err << endl;
		exit(EXIT_FAILURE);
	}

	cout << "Server got connection from " << inet_ntoa(clien_addr.sin_addr) << " port " << ntohs(clien_addr.sin_port) << endl;
}

void Server2::sendMessage(){

	if(send(newsockfd, "Hello, World!\n", 13, 0) < 0){
		int err = WSAGetLastError();
		cout << "Send failed with error " << err << endl;
		perror("Sending failed");
		exit(EXIT_FAILURE);
	}
	bzero(buffer, 256);

}

void Server2::readSocket(){

	if(read(newsockfd, buffer, sizeof(buffer)) < 0){
		int err = WSAGetLastError();
		cout << "Read failed with error " << err << endl;
		perror("Reading failed");
		exit(EXIT_FAILURE);
	}

	cout << "MESSAGE: " << buffer << endl;
}


This is the order of calling methods:
C++
<pre>int main(){

	Server2 server;

	server.createSocket();
	server.clearAdrressStructure();
	server.bindSocket();
	server.listenConnection();
	server.acceptConnection();
	server.sendMessage();
	server.readSocket();

	close(server.newsockfd);
	close(server.sockfd);

	return 0;
}


Sorry for posting entire code. I am doing so because I have no idea whether I'm doing something wrong in the other methods though the error occurs in readSocket() method. Thank you for your valuable time!

What I have tried:

I tried changing the port numbers and using recv() method instead of read()
Posted
Comments
Shao Voon Wong 24-Dec-19 2:37am    
Your code looks correct through send/recv should be used together. My assumption is your client crashed before it can send its message to server, when it displays "Hello, World!" because that string is not null terminated. Try the send code below.

if(send(newsockfd, "Hello, World!\0", 14, 0) < 0){
Richard MacCutchan 24-Dec-19 4:09am    
One of the problems with your code is that all your functions take no parameters and return no results, so it is difficult to see which socket is which. And you should really separate the server code from the client, to make it clear which part is which.

It would also help if you showed the actual output produced when you run the code.

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