Sponsored Content
Full Discussion: Help with sockets in C
Top Forums Programming Help with sockets in C Post 302510064 by omega666 on Friday 1st of April 2011 12:22:35 PM
Old 04-01-2011
server
Code:
#include "header.h"
int main(int argc, char **argv) {
    char buffer[MAXDATASIZE]; 
    bzero(buffer, sizeof(buffer)); 
    client *client_list=NULL; item *item_list=NULL;
    item_list = add_item("bike", "8", "70", "5", "False", item_list);
    int socket_fd, client_socket_fd; 
    socklen_t client_length; 
    struct sockaddr_in serv_addr, cli_addr;
    client_length = sizeof(cli_addr);
    bzero((char *) &serv_addr, sizeof(serv_addr)); 
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_fd < 0) error("ERROR opening socket");
    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(PORT);
    if (bind(socket_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); 
    listen(socket_fd,5);          
    client_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length);
    if (client_socket_fd < 0) error("ERROR on accept"); 
    read_message(client_socket_fd, buffer, 0); 
    if (already_there(client_list, buffer)==0) {      
        client_list = add_client(buffer, client_list, client_socket_fd); 
        send_message(client_socket_fd, "Enter", 5); 
        write(1, buffer, str_len(buffer)); write(1, " connected!\n", 12); 
        read_message(client_socket_fd, buffer, 0); 
        send_items(item_list, client_socket_fd); 
    }
    else {                                       
        send_message(client_socket_fd, "Leave", 5); 
        write(1, "Rejected incoming client - ", 27); write(1, buffer, str_len(buffer)); write(1, "! (User with same name already connected)\n", 42); 
        read_message(client_socket_fd, buffer, 0); 
    }
    close(client_socket_fd); close(socket_fd); 
    return(0); 
}

In this function, this line:
write(1, buffer, str_len(buffer)); write(1, " connected!\n", 12);
is making the client not read everything from the socket
right now, that line is in the right spot to avoid this problem, but if i move that line one line up or 2 lines up, then the client only reads some of the output.

Last edited by omega666; 04-01-2011 at 02:29 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

sockets...

Hi ! I had a verry simple question to ask... In unix when we create pipes.. the unnamed pipes that is... is there any way to access those pipes outside the code ? Another thing.. do sockets have an entry in the inode table ? TIA, Devyani. (1 Reply)
Discussion started by: devy8
1 Replies

2. Programming

Sockets!?!?!?!?!?!

I am looking for a way to have a program listen on a port (example: 8000) for communication I will be sending via that port to it(Linux Kernel machine). Once it recieves an appropiate command I need it to run a .bat file in linux. I know what I need to do but I am running into a few problems:... (8 Replies)
Discussion started by: bigB8210
8 Replies

3. Programming

sockets

Hai, How cani declare socket and collect the data in a string varialbe. Since i am new to this i am asking this. Can we connect multiple port. Thank you. (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

4. UNIX for Dummies Questions & Answers

sockets

how do i mointor how many sockets are opened from a particular foriegn address? (2 Replies)
Discussion started by: kirpond
2 Replies

5. Solaris

Sockets in use

Is there a way to see what sockets are in use? The developers here are getting some defunct processes and they would like to get a socket list. This is on a Solaris 8 machine. Thanks! (1 Reply)
Discussion started by: kjbaumann
1 Replies

6. IP Networking

sockets and firewall

Is it possible to trace the packages and the statuses of client's and/or server's sockets by the UNIX network administrative tools? Two applications interact via sockets. There is no problem if they stay in the same network segment. If their hosts connected through the firewall then they aren't... (4 Replies)
Discussion started by: gogogo
4 Replies

7. Programming

need help with sockets

anyone and teach me how to save standard output to a file in a client/server socket. I know how to read them to the screen but i'm not quite sure how to save them to a file. my read to screen file code: memset(line, 0x0, LINE_ARRAY_SIZE); while (recv(connectSocket, line, MAX_MSG, 0) >... (1 Reply)
Discussion started by: crunchyuser
1 Replies

8. Programming

Sockets

Hi,i now moved into a different section where i need to use sockets. i am completely nill in sockets. can some body please provide me what are the requirements for a socket. to use sockets in c. thanks (1 Reply)
Discussion started by: MrUser
1 Replies

9. Red Hat

Sockets

hai guys, I'm doing a project in which one server communicates with several clients. How can i do it when i have different port numbers???:confused: (0 Replies)
Discussion started by: rajeshb6
0 Replies

10. Programming

Any example about sockets in C++?

Hi, i am student, think learning about c++, someone has a example the how establish a conection with sockets :b::b: (1 Reply)
Discussion started by: mmartinez
1 Replies
explain_connect(3)					     Library Functions Manual						explain_connect(3)

NAME
explain_connect - explain connect(2) errors SYNOPSIS
#include <libexplain/connect.h> const char *explain_connect(int fildes, const struct sockaddr *serv_addr, int serv_addr_size); const char *explain_errno_connect(int errnum, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); void explain_message_connect(char *message, int message_size, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); void explain_message_errno_connect(char *message, int message_size, int errnum, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the connect(2) system call. explain_connect const char *explain_connect(int fildes, const struct sockaddr *serv_addr, int serv_addr_size); The explain_connect function is used to obtain an explanation of an error returned by the connect(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (connect(fildes, serv_addr, serv_addr_size) < 0) { fprintf(stderr, "%s ", explain_connect(fildes, serv_addr, serv_addr_size)); exit(EXIT_FAILURE); } fildes The original fildes, exactly as passed to the connect(2) system call. serv_addr The original serv_addr, exactly as passed to the connect(2) system call. serv_addr_size The original serv_addr_size, exactly as passed to the connect(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_connect const char *explain_errno_connect(int errnum, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); The explain_errno_connect function is used to obtain an explanation of an error returned by the connect(2) system call. The least the mes- sage will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (connect(fildes, serv_addr, serv_addr_size) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_connect(err, fildes, serv_addr, serv_addr_size)); exit(EXIT_FAILURE); } errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the connect(2) system call. serv_addr The original serv_addr, exactly as passed to the connect(2) system call. serv_addr_size The original serv_addr_size, exactly as passed to the connect(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_connect void explain_message_connect(char *message, int message_size, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); The explain_message_connect function may be used to obtain an explanation of an error returned by the connect(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (connect(fildes, serv_addr, serv_addr_size) < 0) { char message[3000]; explain_message_connect(message, sizeof(message), fildes, serv_addr, serv_addr_size); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. fildes The original fildes, exactly as passed to the connect(2) system call. serv_addr The original serv_addr, exactly as passed to the connect(2) system call. serv_addr_size The original serv_addr_size, exactly as passed to the connect(2) system call. explain_message_errno_connect void explain_message_errno_connect(char *message, int message_size, int errnum, int fildes, const struct sockaddr *serv_addr, int serv_addr_size); The explain_message_errno_connect function may be used to obtain an explanation of an error returned by the connect(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (connect(fildes, serv_addr, serv_addr_size) < 0) { int err = errno; char message[3000]; explain_message_errno_connect(message, sizeof(message), err, fildes, serv_addr, serv_addr_size); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the connect(2) system call. serv_addr The original serv_addr, exactly as passed to the connect(2) system call. serv_addr_size The original serv_addr_size, exactly as passed to the connect(2) system call. SEE ALSO
connect(2) initiate a connection on a socket explain_connect_or_die(3) initiate a connection on a socket and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_connect(3)
All times are GMT -4. The time now is 10:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy