-
client/server socket problem
Hi all,
I am using Sun Solaris and GCC. In using AF_UNIX socket, I have established
a conversation by executing "listen()", "accept()", and "read()" to wait
for message from client. When the client try to send more than one message
within the same conversation eg calling three "write()", the "read()" doesn't
hold there waiting for data but returning 0 every time it is executed. I
would like to ask is this how tcp designed to be or I have done something
wrong?
the following is the exact code for server and client.
Thanks and regards,
~~~~~~~~~~~Server~~~~~~~~~~~~~~~
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#define NAME "socket"
main()
{
int sock, msgsock, rval;
struct sockaddr_un server;
char buf[1024];
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, NAME);
if (bind(sock, &server, sizeof(struct sockaddr_un))) {
perror("binding stream socket");
exit(1);
}
printf("Socket has name %s\n", server.sun_path);
listen(sock, 5);
msgsock = accept(sock, 0, 0);
for (;;) {
if (msgsock == -1)
perror("accept");
else do {
bzero(buf, sizeof(buf));
if ((rval = read(msgsock, buf, 1024)) < 0)
perror("reading stream message");
else if (rval == 0)
printf("Ending connection\n");
else
printf("-->%s\n", buf);
} while (rval > 0);
}
close(msgsock);
close(sock);
unlink(NAME);
}
~~~~~~~~~~~~Client~~~~~~~~~
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#define DATA "Half a league, half a league . . ."
main(argc, argv)
int argc;
char *argv[];
{
int sock;
struct sockaddr_un server;
char buf[1024];
if( argc != 2 )
{
printf("Usage: unix_client <socket Name>\n");
exit(-1);
}
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, argv[1]);
if (connect(sock, &server, sizeof(struct sockaddr_un)) < 0) {
close(sock);
perror("connecting stream socket");
exit(1);
}
if (write(sock, DATA, sizeof(DATA)) < 0)
perror("writing on stream socket");
if (write(sock, DATA, sizeof(DATA)) < 0)
perror("writing on stream socket");
if (write(sock, DATA, sizeof(DATA)) < 0)
perror("writing on stream socket");
exit(0);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks