LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2014, 09:29 AM   #1
c developer
LQ Newbie
 
Registered: Mar 2014
Posts: 1

Rep: Reputation: Disabled
problem in my tcp ip client server program,suggest me .


Quote:
Good evening.. I am working on tcpip server client program in C - redhat. Problem here is my code dont has error but i guess there is some connection problem or tags. I cant able to figure the exact problem
Code:
void socket_server_demo(void){
	int iserver_socket = -1;
	int iclient_socket = -1;
	unsigned char bTemp[1024];
	unsigned int  iTemp = 0;
	int i;
	int active_net = 0;
	char client_addr[255];
	unsigned int client_port = 0;
	char iflist[16][IFNAMSIZ];
	struct ifreq ifr;
	int error;
	
	clean();
	printf(ALG_CENTER, "socket server");
	iTemp = netif_get_list(iflist, sizeof(iflist)/IFNAMSIZ);
	for (i = 0; i < iTemp; i++){
		memset(&ifr, 0, sizeof(ifr));
		netif_get_flags(iflist[i], &ifr);
	if (ifr.ifr_flags & IFF_RUNNING){
			netif_get_ipaddr(iflist[i], &ifr);
			printf(ALG_CENTER, "Address:%s:%d", inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr), SERVER_PORT);
			active_net++;}}
	if (active_net == 0){
		printf(ALG_CENTER, "NO Active net work");	
		kb_getkey();
		return;}
	iserver_socket = tcp_server(SERVER_PORT, 1);
	if (iserver_socket >=0){
		lcd_printf(ALG_LEFT, "Wait connection on %d...",SERVER_PORT);
		while (1)
		{
			printf(ALG_LEFT, "inside loop");
			if (kb_hit()){
				if (0x1B == kb_getkey())
					break;
			}

	iclient_socket = tcp_accept(iserver_socket, client_addr, &client_port);
			if (iclient_socket < 0)
				continue;
			else {
		printf(ALG_LEFT, "Connected from %s:%d...", client_addr, client_port);
				while (1){
					if (kb_hit()){
						if (0x1B == kb_getkey())
						break;
					}
					iTemp = sizeof(bTemp);
					error =  tcp_read(iclient_socket, bTemp, &iTemp);if (error == 0){
						if (iTemp > 0)
						{
				printf(ALG_LEFT, "Readed %d bytes", iTemp);
												error = tcp_write(iclient_socket, bTemp, iTemp);
							if (error != 0){
								lcd_printf(ALG_LEFT, "connection was broken");
								break;
	}else {usleep(100000);
						}
					}else {
					printf(ALG_LEFT, "connection was broken");
						break;
					}
				}
				tcp_close(iclient_socket);
				iclient_socket = -1;
			}
			printf(ALG_LEFT, "Wait connection on %d...", SERVER_PORT);
				}
		tcp_close(iserver_socket);
		iserver_socket = -1;
	}else {
		kb_getkey();
	}
}

Code:
#define BufferLength 100
#define SERVER "127.0.0.1"
#define SERVPORT 9090
//socket_client_demo
void socket_client_demo(void){
	int iclient_socket = -1;
	unsigned char bTemp[1024];
	unsigned int  iTemp;
	printf(ALG_CENTER, "socket client");

	printf(ALG_LEFT, "connect to %s:%d...", SERVER_ADDR, SERVER_PORT);
	lcd_flip();

	iclient_socket = tcp_connect(SERVER_ADDR, SERVER_PORT);
	if (iclient_socket < 0){
		printf(ALG_LEFT, "Connect failed.");
		kb_getkey();
	}else {
		while (1){
			if (kb_hit()){
				if (0x1B == kb_getkey())
					break;
			}
			strcpy((char*)bTemp, "HELLO WORLD");	
			iTemp = strlen((char*)bTemp);
			if (tcp_write(iclient_socket , bTemp, iTemp) < 0){
				lcd_printf(ALG_LEFT, "connection was broken");
				kb_getkey();
				break;
			}else{
				usleep(500000);
				iTemp = sizeof(bTemp);
				if (tcp_read(iclient_socket, bTemp, &iTemp) < 0){
					printf(ALG_LEFT, "connection was broken");
					kb_getkey();
					break;
				}else if (iTemp > 0){
				printf(ALG_LEFT, "Readed %d bytes", iTemp);
				}else{
				}}	}
		tcp_close(iclient_socket);}}


Quote:
void tcp_init(void)
{
static int inited = 0;
if (!inited){
signal(SIGPIPE, SIG_IGN);
inited = 1;
}
}


int tcp_server(unsigned int uiPort, unsigned int MaxConnect)
{
#define MAX_SOCKETS 1000
int isocket = -1;
struct sockaddr_in servaddr;
tcp_init();
if (MaxConnect == 0)
MaxConnect = MAX_SOCKETS;
isocket = socket(AF_INET, SOCK_STREAM, 0);
if (isocket >= 0){
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(uiPort);
if( bind(isocket, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
tcp_close(isocket);
isocket = -2;
}else if( listen(isocket,MaxConnect ) == -1){
tcp_close(isocket);
isocket = -3;
}
}
return isocket;
}

int tcp_accept(int iSvrSocket, char szClientIP[16], unsigned int * puiClientPort)
{
int clientSocket = -1;
struct timeval tv;
struct sockaddr_in addr;
#if defined(WIN32)
int addrlen;
#else
unsigned int addrlen;
#endif
fd_set r;
tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&r);
FD_SET(iSvrSocket, &r);
if (select(iSvrSocket + 1, &r, NULL, NULL, &tv)>0)
{
addrlen = sizeof(struct sockaddr);

clientSocket=accept(iSvrSocket, (struct sockaddr *)&addr, &addrlen);
if (clientSocket >=0)
{
if (szClientIP != NULL)
{
sprintf(szClientIP, inet_ntoa( addr.sin_addr));
}
if (NULL != puiClientPort)
{
*puiClientPort = ntohs(addr.sin_port);
}
}
}
return clientSocket;
}

int tcp_connect(const char * pServerAddr, unsigned int port)
{
int clientsocket = -1;
struct sockaddr_in addr;
struct hostent *pHE;
tcp_init();
clientsocket = socket(AF_INET,SOCK_STREAM,0);
if (-1 != clientsocket){

memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr=inet_addr(pServerAddr);
if (INADDR_NONE == addr.sin_addr.s_addr)
{
pHE=gethostbyname(pServerAddr);
if(pHE)
{
addr.sin_addr.s_addr = *(u_long *) pHE->h_addr_list[0];
}
}

addr.sin_family=AF_INET;
addr.sin_port=htons(port);

if(connect(clientsocket,(struct sockaddr *)&addr, sizeof(addr))==0)
{

}
else
{
tcp_close(clientsocket);
clientsocket = -1;
}

}
return clientsocket;
}

void tcp_close(int isocket)
{
if (isocket >=0)
close(isocket);
}

int tcp_read(int isocket, void * pvbuff, unsigned int * pisize)
{
int retval = 0;
int error;
struct timeval tv;
fd_set r;


tv.tv_sec = 0;
tv.tv_usec = 1000;

FD_ZERO(&r);
FD_SET(isocket, &r);
error= select(isocket+1,&r,NULL, NULL, &tv);

if ( error > 0)
{

if (*pisize > 0)
{
error = recv(isocket,(char*)pvbuff, *pisize, 0);
if (error > 0)
{
*pisize = (unsigned int)error;
}
else
{
retval = -1;
}
}
}
else if (0 != error)
{
retval = -1;
}
else
{
*pisize = 0;
retval = 0;
}

return retval;
}

int tcp_write(int isocket, const void * pvdata, unsigned int isize)
{
int retval = 0;
struct timeval tv;
fd_set r;

tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&r);
FD_SET(isocket, &r);

if (select(isocket+1,NULL,&r,NULL,&tv) > 0)
{
if(isize != (unsigned int)send(isocket,(char*)pvdata, isize, 0))
{
retval = -1;
}
}
else
{
retval = -1;
}

return retval;
}

Last edited by c developer; 03-22-2014 at 09:34 AM. Reason: this is my entire tcp ip program , i am not able to connect , i am trying but connection is not happened
 
Old 03-22-2014, 09:46 AM   #2
redd9
Member
 
Registered: Nov 2013
Location: Canada
Distribution: Ubuntu
Posts: 80
Blog Entries: 4

Rep: Reputation: 42
Sorry, but I don't know what you mean. Your code 'dont has error'? Do you mean it won't compile? In that case, please try to narrow the problem down, because that is way too much code to sort through. If the problem is your program won't work, what happens exactly?
 
1 members found this post helpful.
  


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
Tcp/ip socket server and client program - Redhat linux server 5(eclipse CDT) parvathi reddy Linux - Newbie 2 03-16-2014 02:51 AM
Client Server Socket TCP/IP program in C Linux over HTTPS fahad.anwar Linux - Newbie 6 05-29-2012 03:59 AM
Client Server Socket TCP/IP program in C Linux using threads fahad.anwar Linux - Newbie 2 05-18-2012 04:34 AM
[question] a tcp server and client program silentkill89 Programming 7 09-11-2011 12:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration