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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-24-2005, 02:03 AM
#1
LQ Newbie
Registered: Apr 2005
Posts: 9
Rep:
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
05-24-2005, 02:33 AM
#2
LQ Newbie
Registered: Apr 2005
Posts: 9
Original Poster
Rep:
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
05-24-2005, 02:38 AM
#3
LQ Newbie
Registered: Nov 2004
Location: Istanbul
Distribution: Slackware 10.2
Posts: 19
Rep:
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 09:16 PM .
05-24-2005, 02:50 AM
#4
LQ Newbie
Registered: Nov 2004
Location: Istanbul
Distribution: Slackware 10.2
Posts: 19
Rep:
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 09:17 PM .
05-24-2005, 03:04 AM
#5
LQ Newbie
Registered: Apr 2005
Posts: 9
Original Poster
Rep:
halitayarci
Thank you very much
All times are GMT -5. The time now is 11:20 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News