LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-02-2002, 12:09 PM   #1
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Rep: Reputation: 15
Cool Code :)


Just compile the programs

gcc -o client client.c

and

gcc -o server server.c

and run the server in the back ground with

./server &
press enter a couple times then

./client

follow instructions and watch them talk ))







//Client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>

int main ()
{
int sockfd;
int len;
struct sockaddr_un address;
int result;
char ch;
int x;
unsigned long u;

//get the character to send
printf("Enter up to 100 characters to send\n");
for(x=0;x<=100;x++)
{
scanf("%c", &ch);

//create a socket for the client
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

//Name the socket as agreed with the server
address.sun_family=AF_UNIX;
strcpy(address.sun_path,"server_socket");
len = sizeof(address);

//Now connect our socket to the server's socket
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result==-1) {
perror("oops: client1");
exit (1);
}

//Now we can read and write via sockfd
printf("\nClient: Ready to write %c to socket\n", ch);
write(sockfd, &ch, 1);
printf("Client: Wrote %c to socket. \n", ch);
read(sockfd, &ch, 1);
printf("Client: Received char from server = %c\n", ch);

//Close the socked
close(sockfd);
}
return (0);
}//end of main




//Server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>

int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;

//Remove any old socket and create an unnamed socket for the server
unlink("server_socket");
server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

//Name the socket
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

//Create a connection queue and wait for clients.
listen(server_sockfd, 6);
while(1) {
char ch;

printf("Server: Waiting...\n");

//Accept a connection.
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address, &client_len);

//Read and write to client onclient_sockfd
printf("Server: Getting ready to read\n");
read(client_sockfd, &ch, 1);
printf("Server: Read the character %c\n", ch);
printf("Server: Incremented the character %c to", ch);
ch++;
printf("%c\n", ch);
printf("Server: Ready to write the character %c back\n", ch);
write(client_sockfd, &ch, 1);
printf("Server: Character %c was written.\n", ch);
close(client_sockfd);
}
}
 
Old 05-02-2002, 02:59 PM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
I haven't tried your code but I'm sure it will work.
Just a question though why are you opening and closing the connection for each character you send. Much more efficient to connect only once and then send all the data then disconnect.
 
Old 05-07-2002, 02:37 PM   #3
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Original Poster
Rep: Reputation: 15
How would I accomplish this..
Just remove the code that closes
the connection?
 
Old 05-08-2002, 04:53 PM   #4
Snake007uk
Member
 
Registered: May 2002
Location: London
Distribution: Redhat 7.3
Posts: 84

Rep: Reputation: 15
ive seen that code b4 (not implying anything! ) are your from middlesex uni in uk, coz i was shown that code by a tutor its a client server program that send words/characters via socktes (basic socket programming)

Snake
 
Old 05-08-2002, 05:07 PM   #5
Winter
LQ Newbie
 
Registered: May 2002
Location: Washington State
Distribution: Red Hat 7.0
Posts: 22

Original Poster
Rep: Reputation: 15
I live in Vancouver Washington in the US..

It is a very simple piece of code to create..

just a cool one at that!

Winter
 
  


Reply



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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Diffrerence between position independent code and Relocatable code? eshwar_ind Programming 7 05-11-2004 01:40 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:04 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
Open Source Consulting | Domain Registration