套接字编程:无法显示输出! [英] Socket Programming : Can't able to display output !

查看:102
本文介绍了套接字编程:无法显示输出!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..我是C ++的新手,这是基本客户端和服务器的代码.(我正在尝试编写聊天程序).

服务器:

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 :

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

#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)..!!

推荐答案

我在您的代码中看到两件事:

(a)您只收到缓冲区长度为32的缓冲区.这些将从服务器答复中的ASCII填充(长度为256个字节).这32个字节后面的存储空间是不确定的.也许您想在那里接收256个字节,并将服务器的响应限制为strlen(sendbuffer)+1.

(b)收到内容后的cout语句不包含\ n.因此,当您终止程序时,您的输出可能仍在stdio缓冲区中.
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.


这篇关于套接字编程:无法显示输出!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆