Socket programming is very familiar to everyone. I will not be arrogant here. Directly go to the topic. TheProgramThere are two parts: server and client (everyone knows ). The server uses the Windows platform, and the Linux platform is of course the client.
Here is the server program:
/**********************************/
/* It worked as server under Windows platform */
/* Written by mlsx 1998-2-7 */
/************************************/
Include <windows. h>
# Include <stdio. h>
# Pragma comment (Lib, "ws2_32.lib ")
# Define userport 10001
# Define host_ip_addr "127.0.0.1"
Main (INT argc, char * argv [])
{
Char Buf [128];
Socket S, ns;
Struct sockaddr_in client;
Struct sockaddr_in server;
Int namelen, pklen;
Int status;
Wsadata WSD;
If (status = wsastartup (makeword (2, 2), & WSD ))! = 0)
{
Perror ("wsastartup () failed :");
Exit (1 );
}
If (S = socket (af_inet, sock_stream, 0) <0)
{
Perror ("socket failed :");
Exit (1 );
}
Zeromemory (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (userport );
Server. sin_addr.s_addr = htons (inaddr_any );
If (BIND (S, (struct sockaddr *) & server, sizeof (server) <0)
{
Perror ("BIND () failed :");
Exit (2 );
}
If (Listen (S, 4 )! = 0)
{
Perror ("Listen () failed :");
Exit (3 );
}
While (1)
{
Namelen = sizeof (client );
If (NS = accept (S, (struct sockaddr *) & client, & namelen) =-1)
{
Perror ("accept () failed :");
Exit (4 );
}
Printf ("accept successful ");
For (;;)
{
If (pklen = Recv (NS, Buf, 128,0) <0)
{
Perror ("Recv () failed :");
Exit (5 );
}
Else
If (pklen = 0)
{
Printf ("Recv (): return failed, connected is shut down ");
Break;
}
Else
Printf ("Recv (): Return success, packet length = % d context is % s/n", pklen, Buf );
Sleep (1 );
// Printf ("The Buf is % s/n", Buf );
If (send (NS, Buf, 128,0) <0)
{
Perror ("Send () failed :");
Break;
}
Else
Printf ("Send () return success packet length is % d/N", pklen );
}
}
Closesocket (NS );
Closesocket (s );
Printf ("server ended successfully/N ");
}
Here is the client program:
/**********************************/
/* It worked as client under Linux platform */
/* Written by mlsx 1998-2-7 */
/************************************/
# Include <windows. h>
// # Include <winsock2.h>
# Include <stdio. h>
# Pragma comment (Lib, "ws2_32.lib ")
# Define userport 5003
Main (INT argc, char * argv [])
{
Char Buf [128];
Socket sclient;
Struct sockaddr_in server;
Int namelen, pklen;
Int status, RET;
Char szmessage [128];
Wsadata WSD;
If (argc <2)
{
Printf ("Usage:/N ");
Printf ("% s ipaddr" ,__ file __);
Printf ("ipaddr is the server's IP address, like XXX. XXX/N ");
Exit (1 );
}
If (status = wsastartup (makeword (2, 2), & WSD ))! = 0)
{
Perror ("wsastartup () failed :");
Exit (1 );
}
If (sclient = socket (af_inet, sock_stream, 0) <0)
{
Perror ("socket failed :");
Exit (1 );
}
Zeromemory (& server, sizeof (server ));
Zeromemory (szmessage, sizeof (szmessage ));
Server. sin_family = af_inet;
Server. sin_port = htons (userport );
Server. sin_addr.s_addr = inet_addr (argv [1]);
If (connect (sclient, (struct sockaddr *) & server, sizeof (server) <0)
{
Perror ("Connect () failed :");
Printf ("% d", wsagetlasterror ());
Exit (3 );
}
Namelen = sizeof (sclient );
/* For (ret = 0; RET <10; RET ++)
{
If (send (sclient, szmessage, sizeof (szmessage), 0) <0)
{
Perror ("Send () failed :");
Break;
}
Printf ("Send () return success/n context is % s/n", szmessage );
If (pklen = Recv (sclient, Buf, sizeof (BUF), 0) <0)
{
Perror ("Recv () failed :");
Exit (5 );
}
Else
If (pklen = 0)
{
Printf ("Recv (): return failed, connected is shut down ");
Break;
}
Else
{
Printf ("The pktlen is % d/N", pklen );
Buf [pklen] = '/0 ';
Printf ("Recv (): Return success/npacket length = % d and the context is % s/n", pklen, Buf );
Zeromemory (BUF, sizeof (BUF ));
}
Sleep (10 );
}*/
// While (strcmp (szmessage, "Q "))
While (1)
{
Zeromemory (szmessage, sizeof (szmessage ));
// Scanf ("% s", szmessage );
// Puts (szmessage );
// Gets (szmessage );
// Send (sclient, szmessage, sizeof (szmessage), 0 );
// Sleep (10 );
Zeromemory (BUF, sizeof (BUF ));
Ret = Recv (sclient, Buf, sizeof (BUF), 0 );
Buf [RET] = '/0 ';
// Printf ("% s the length is % d", Buf, RET );
Puts (BUF );
Sleep (1 );
}
Closesocket (sclient );
Wsacleanup ();
Printf ("server ended successfully/N ");
Return 0;
}