Writing with Server in C++

<p>I am programming a server-client service, the client is in Java and the Server is in C++, but when I try to write by the Server to the Client, he does not sends the message, only when i close the socket, the server sends the message and the client reads it. Here is the Server Code:</p><pre><br /><code>#include<stdio.h><br />#include<stdlib.h><br />#include<sstream><br />#include<string><br />#include<netdb.h><br />#include<netinet/in.h><br />#include<string.h><br />#include<iostream><br />#include<sys/types.h><br />#include<sys/socket.h><br />#include<arpa/inet.h><br />#include<unistd.h><br /><br /><br />using namespace std;<br /><br />void doprocessing (int sock, int cliente);<br />void sendM(int socket,int client);<br /><br />int main(int argc, char *argv[]) {<br /> int sockfd, newsockfd, portno,cliente;<br /> char buffer[256];<br /> struct sockaddr_in serv_addr, cli_addr;<br /> int n, pid;<br /> socklen_t clilen;<br /><br /><br /> sockfd = socket(AF_INET, SOCK_STREAM, 0);<br /><br /> if (sockfd < 0) {<br /> perror("ERROR opening socket");<br /> exit(1);<br /> }<br /><br /> bzero((char *) &serv_addr, sizeof(serv_addr));<br /> portno = 8080;<br /><br /> serv_addr.sin_family = AF_INET;<br /> serv_addr.sin_addr.s_addr = INADDR_ANY;<br /> serv_addr.sin_port = htons(portno);<br /><br /> if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {<br /> perror("ERROR on binding");<br /> exit(1);<br /> }<br /><br /><br /> listen(sockfd,5);<br /> clilen = sizeof(cli_addr);<br /> cout<<"Servidor Iniciado1..."<<endl;<br /><br /> while (1) {<br /> newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);<br /> cliente=cliente+1;<br /> if (newsockfd < 0) {<br /> perror("ERROR aceptando conexion");<br /> exit(1);<br /> }<br /> /* Create child process */<br /> pid = fork();<br /><br /> if (pid < 0) {<br /> perror("ERROR on fork");<br /> exit(1);<br /> }<br /><br /> if (pid == 0) {<br /><br /> cout<<"Nueva conexion "<<cliente<<endl;<br /> close(sockfd);<br /> doprocessing(newsockfd,cliente);<br /> exit(0);<br /> }<br /> else {<br /> close(newsockfd);<br /> }<br /><br /> }<br />}<br /><br />void doprocessing (int sock, int cliente1) {<br /> int n;<br /> char buffer1[256];<br /> bzero(buffer1,256);<br /> n = read(sock,buffer1,255);<br /> n=1;<br /> if (n < 0) {<br /> perror("ERROR leyendo el socket");<br /> exit(1);<br /> }<br /> cout<<"Mensaje de cliente "<<cliente1<<":"<<buffer1<<endl;<br /> sendM(sock,cliente1);<br /> if (n < 0) {<br /> perror("ERROR escribiendo al socket");<br /> exit(1);<br /> }<br /><br />}<br /><br />void sendM(int socket, int client){<br /> std :: string buff;<br /> int n;<br /> cout << "\nServidor: ";<br /> cin >> buff;<br /> n=send(socket, buff.data(), buff.length(),0);<br /> cout<<buff.data()<<"\n";<br /> cout<<n<<endl;<br /> if(n<0){<br /> cout<<"Error enviando respuesta"<<endl;<br /> }<br /> doprocessing(socket,client);<br />}<br /></code><br /></pre><br /><h2>Click here view more answers <a href="http://ift.tt/1L7Se1F">http://ift.tt/1L7Se1F</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading