LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-06-2012, 10:20 PM   #1
fachamix
Member
 
Registered: Oct 2009
Posts: 122

Rep: Reputation: 16
Socket problem - C/Linux


I have the following code.
Dont pay atention to the SPANISH outputs (I am from Argentina).

The problem is that when I run it, starts fine, and waits for a client to connect.
Once the client connects, the client can send a datablock.
Once received (datablock) the server automaically hungs and exits.

here the code...

Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    printf("Iniciando servidor ...\n\r");
    int server_sockfd, client_sockfd;
    struct sockaddr_in server_dir, client_dir;
    int server_len, client_len;
    char buffer[1024];

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server_dir.sin_family = AF_INET;
    server_dir.sin_addr.s_addr = inet_addr("10.0.0.4");
    server_dir.sin_port = htons(9981);

    server_len = sizeof(server_dir);
    bind(server_sockfd,(struct sockaddr*)(&server_dir),server_len);

    listen(server_sockfd,5);

    while (1)
    {
        printf("Esperando clientes ...\n\r");

        client_len = sizeof(client_dir);
        client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_dir,(socklen_t*)&client_len);
        if (client_sockfd == -1)
        {
            perror("DEBUG");
            exit(EXIT_FAILURE);
        }
        printf("Cliente conectado, esperando datos ...\n\r");
        while (1)
        {
            memset(buffer,'\0',sizeof(buffer));
            int temp = read(client_sockfd,buffer,sizeof(buffer));
            buffer[strlen(buffer) - 2] = '\0';
            printf("Leidos %d \n\r",temp);
            printf("Datos recibidos [%s]\n\r",buffer);
            if (strcmp(buffer,"quit") == 0)
            {
                break;
            }
            printf("Enviando respuesta de ECHO\n\r"); //ESTO SI SE EJECUTA
            strcpy(buffer,"DATOS-RECIBIDOS"); //ESTO TAMBIEN
            write(server_sockfd,buffer,sizeof(buffer));//I BELIEVE HERE IS THE PROBLEM BUT I ITS JUST A THOUGHT, I DONT KNOW HATS WRONG.
        }
        close(client_sockfd);
        printf("Fin de conexion.\n\r");
    }
    close(server_sockfd);
    exit(EXIT_SUCCESS);
}
HOW I EXECUTE IT ?

I compile this code an run the server.

once compiled and executed, I open a new terminal, and execute "telnet <IP> <PORT>" in this case "telnet 10.0.0.4 9981" , and I can succesfully connect, but I can only send 1 packet and then the server terminates.

Please, need some advice.
 
Old 01-07-2012, 02:25 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,946

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Code:
old: write(server_sockfd,buffer,sizeof(buffer));
new: write(client_sockfd,buffer,strlen(buffer));
 
Old 01-07-2012, 07:27 AM   #3
fachamix
Member
 
Registered: Oct 2009
Posts: 122

Original Poster
Rep: Reputation: 16
thanks very very much my friend.

it works perfectly now.


I should not being programming hard at night next time

thanks.


good bye
 
Old 01-07-2012, 02:02 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
One additional suggestion:

Code:
write(client_sockfd,buffer,strlen(buffer)+1);
 
Old 01-07-2012, 05:42 PM   #5
fachamix
Member
 
Registered: Oct 2009
Posts: 122

Original Poster
Rep: Reputation: 16
thanks you too for the sug.
 
  


Reply

Tags
linux, problem, socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
socket - select() timeout problem in Linux Tejesh Linux - Networking 2 06-10-2009 05:38 AM
Linux - Windows internet socket problem entz Programming 24 06-13-2007 08:49 PM
Problem with Linux socket filter shreekantsingh Linux - Networking 0 05-15-2006 10:08 AM
problem using socket in perl for linux enviornment hitesh29 Linux - Networking 0 04-08-2004 01:11 AM


All times are GMT -5. The time now is 06:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration