LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-24-2005, 03:03 AM   #1
jawadhashmi
LQ Newbie
 
Registered: Apr 2005
Posts: 9

Rep: Reputation: 0
Socket Programming


Hi,

I have developed an application using VC++ using win32 API. This is customised email server which received and sends emails. I want to port that server application in Linux.

There we have winsoc for socket programming. How i can do this in Linux.
Please give me any useful link for socket programming in Linux.

Thanks
 
Old 05-24-2005, 03:33 AM   #2
jawadhashmi
LQ Newbie
 
Registered: Apr 2005
Posts: 9

Original Poster
Rep: Reputation: 0
I have found a link and i am going through it. Netwrok Programming

If any one else has good or helpful material please post it here for me and many others.
Thanks
 
Old 05-24-2005, 03:38 AM   #3
halitayarci
LQ Newbie
 
Registered: Nov 2004
Location: Istanbul
Distribution: Slackware 10.2
Posts: 19

Rep: Reputation: 0
echo server code in C

Code:
/***************************************************************************
 *   Copyright (C) 2005 by halit                                           *
 *   halit@localhost                                                       *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>      /* for printf() and fprintf() 		     	*/
#include <sys/socket.h> /* for socket(), connect(), send() and recv()	*/ 
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr()		*/
#include <stdlib.h>     /* for atoi()                         		*/
#include <string.h>     /* for memset()                  		*/
#include <unistd.h>     /* for close()                   		*/

/* Maximum outstanding conn. requests */
#define MAXPENDING 5
#define RCVBUFSIZE 32

void DieWithError(char *errorMessage); /* error handling func. 			*/
void HandleTCPClient(int clntSocket);  /* for TCP client handling function 	*/

int main(int argc, char *argv[])
{
	int servSock, clntSock;
	struct sockaddr_in echoServAddr;
	struct sockaddr_in echoClntAddr; 
	unsigned short echoServPort;
	unsigned int clntLen;   /* Length of client address data structure */

	echoServPort = 7; /* well-known port for echo service */
	
	/* Create socket for incoming connections */
	if((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		DieWithError( "socket() failed\n" );
	
	/* Construct Local address structure */
	memset(&echoServAddr, 0, sizeof(echoServAddr));
	echoServAddr.sin_family = AF_INET;
	echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	echoServAddr.sin_port = htons(echoServPort);

	/* Bind to the local address */
	if(bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
		DieWithError( "bind() failed\n" );
	
	/* Mark the socket so it will listen for incoming connections */
	if(listen(servSock, MAXPENDING) < 0)
		DieWithError( "listen() failed\n" );
	for(;;)
	{
		printf("listening\n");
		/* Wait for a client to connect */
		if((clntSock = accept(servSock, NULL, NULL) < 0))
			DieWithError( "bind() failed\n" );
	
		/* clntSock is connected to a client */
		printf( "Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
	
		HandleTCPClient(clntSock); 
	}
	/* NOT REACHED */
}

void DieWithError(char *errorMessage)	
{
	printf(errorMessage);
	exit(0);
}

void HandleTCPClient(int clntSocket)	
{
	char echoBuffer[RCVBUFSIZE];
	int recvMsgSize;

	/* Receive message from client */
	if((recvMsgSize=recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) 
		DieWithError("recv() failed\n");
		
	/* Send received string and receive again until the end of transmission */
	while(recvMsgSize > 0)   /* zero indicates end of transmission */  
	{
		/* Echo message back to client */
		if((send(clntSocket, echoBuffer, recvMsgSize, 0)) != recvMsgSize)
			DieWithError("send() failed\n");
		
		/* Receive message from client */
		if((recvMsgSize=recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) 
			DieWithError("recv() failed\n");
	}	
	close(clntSocket);
}

Last edited by halitayarci; 04-10-2006 at 10:16 PM.
 
Old 05-24-2005, 03:50 AM   #4
halitayarci
LQ Newbie
 
Registered: Nov 2004
Location: Istanbul
Distribution: Slackware 10.2
Posts: 19

Rep: Reputation: 0
this one is a tcp client written in C

Code:
/***************************************************************************
 *   Copyright (C) 2005 by halit                                           *
 *   halit@localhost                                                       *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>      /* for printf()                    */
#include <sys/socket.h> /* for socket(), connect(), send() and recv() */ 
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi()                      */
#include <string.h>     /* for memset()                    */
#include <unistd.h>     /* for close()                     */

/* Size of receive buffer */
#define RCVBUFSIZE 32

void DieWithError(char *errorMessage); /* error handling function */

int main(int argc, char *argv[])
{
	int sock;
	struct sockaddr_in echoServAddr;
	unsigned short echoServPort;
	char *servIP;
	char *echoString;
	char echoBuffer[RCVBUFSIZE];
	unsigned int echoStringLen;
	int bytesRcvd, totalBytesRcvd;

	if (argc != 3)
	{
		printf( "Usage: %s <Server IP> <Echo Word> \n" , argv[0]);
		exit(1);
	}
	servIP = argv[1];
	echoString = argv[2];
	echoServPort = 7;   /* well-known port for echo service */
	
	/* Create a reliable, stream socket using TCP */
	if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		DieWithError( "socket() failed\n" );
  
	/* Construct the server address structure */
	memset(&echoServAddr, 0, sizeof(echoServAddr));
	echoServAddr.sin_family = AF_INET;
	echoServAddr.sin_addr.s_addr = inet_addr(servIP);
	echoServAddr.sin_port = htons(echoServPort);
	
	/* Establish a connection to the echo cerver */
	if(connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
		DieWithError( "socket() failed\n" );
	
	echoStringLen = strlen(echoString); /*Determine the input length */
	
	/* Send the string to the server */
	if((send(sock, echoString, echoStringLen, 0)) != echoStringLen)
		DieWithError( "send() failed\n" );
	
	/* receive the same string back from the server */
	if((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
		DieWithError( "socket() failed\n" );
	
	printf(echoBuffer); 
	
	printf( "\n" );
	close(sock);
	exit(0);
}

void DieWithError(char *errorMessage)
{
	printf(errorMessage);
	exit(0);
}

Last edited by halitayarci; 04-10-2006 at 10:17 PM.
 
Old 05-24-2005, 04:04 AM   #5
jawadhashmi
LQ Newbie
 
Registered: Apr 2005
Posts: 9

Original Poster
Rep: Reputation: 0
halitayarci
Thank you very much
 
  


Reply


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
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 05:50 PM
socket programming???? harbir Linux - Networking 2 07-05-2004 03:52 AM
want help in socket programming valib4u *BSD 2 09-12-2003 12:17 AM
Socket Programming cxel91a Programming 4 03-19-2003 11:05 AM
socket programming herambshembekar Programming 3 04-13-2002 04:13 PM


All times are GMT -5. The time now is 08:22 PM.

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