Demo of TCP communication between Java client and C ++ Server

Source: Internet
Author: User
Java client code creates socket binding import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. printwriter; import java.net. inetsocketaddress; import java.net. socket; public class javaclient {public static void main (string [] ARGs) throws interruptedexception, ioexception {inetsocketaddress ADDR = new inetsocketaddress ("Wagner. 0.0.1", 5678 ); // create socketsocket sock = new socke T (); sock. connect (ADDR); // connection server bufferedreader is = NULL; printwriter OS = NULL; OS = new printwriter (sock. getoutputstream (); // pipeline write tool is = new bufferedreader (New inputstreamreader (sock. getinputstream (); // The pipeline read tool bufferedreader user = new bufferedreader (New inputstreamreader (system. in); // The system tool that reads commands from the console. out. print ("client say:"); string MSG = user. readline (); While (! MSG. equals ("bye") {OS. println (MSG); OS. flush (); string echo = is. readline (); system. out. println (ECHO); system. out. print ("client say:"); MSG = user. readline ();} OS. close (); is. close (); sock. close ();}}

C ++ server, written in previous articles, copied now

Create socket

Bind Port

Listening port

// Leleserver. cpp: defines the entry point of the console application. // # Include <winsock2.h> # include <process. h ># Pragma comment (Lib, "ws2_32.lib") # include <list> STD: List <socket> client_list; void thread_client (void *); int main (INT argc, char * argv []) {wsadata; wsastartup (0x22, & wsadata); unsigned short sport = argc> 1? Atoi (argv [1]): 5678; int sockfd = socket (af_inet, sock_stream, ipproto_tcp); If (sockfd = invalid_socket) {printf ("fail to create socket. \ n "); return 1;} sockaddr_in saddr; saddr. sin_addr.s_addr = addr_any; saddr. sin_port = htons (sport); saddr. sin_family = af_inet; If (BIND (sockfd, (const sockaddr *) (& saddr), sizeof (saddr) {printf ("fail to bind, errno: % d. \ n ", getlasterror (); dword dw = wsagetlasterror (); Return 2;} If (Listen (sockfd, 5) {printf ("fail to listen. \ n "); return 3;} printf (" server is listening at [% d]. \ n "" wating for client to connect... \ n ", sport); For (;) {sockaddr_in caddr; int caddr_len = sizeof (caddr); int client = accept (sockfd, (sockaddr *) & caddr, & caddr_len); unsigned char * paddr = (unsigned char *) & caddr. sin_addr.s_addr; printf ("[% d. % d. % d. % d: % d] Coming. \ n ", paddr [0], paddr [1], Pa DDR [2], paddr [3], ntohs (caddr. sin_port); _ beginthread (thread_client, 0, (void *) client); client_list.push_back (client);} return 0;} void thread_client (void * ARGs) {Socket Client = (INT) ARGs; sockaddr_in caddr; int caddr_len = sizeof (caddr); getpeername (client, (sockaddr *) & caddr, & caddr_len ); unsigned char * paddr = (unsigned char *) & caddr. sin_addr.s_addr; unsigned short usport = ntohs (caddr. sin_port); (;) {Char szmsg [0x1000] = ""; if (0> = Recv (client, szmsg, sizeof (szmsg)-1, 0) break; char szecho [0x1000] = ""; sprintf (szecho, "server say: [% d. % d. % d. % d: % d] % s ", paddr [0], paddr [1], paddr [2], paddr [3], usport, szmsg); For (STD :: list <socket >:: iterator it = client_list.begin (); it! = Client_list.end (); It ++) {send (* It, szecho, strlen (szecho), 0 );}}}

The communication result is displayed in eclipse.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.