JAVA and C # TCP communication--java Client, C # thread pool server __java

Source: Internet
Author: User
Tags flush getstream readline sendmsg

A small example, used for Java and C # TCP communication, Java as a client,c# as a server,server thread pool.

The main function is that the client can send data to the server, and then the server sends data to the client, disconnected;

Corresponds to the client issue request, the server gives a response.

JAVA Client:

Package com.cn.test;
Import java.net.*;

Import java.io.*;
    public class TCPClient2 {public static void main (string[] args) throws Exception {TcpClient ();
        public static void TcpClient () {String s= "";
        Socket Mysocket;
        InputStreamReader In=null;
        
        OutputStreamWriter Out=null;
            try{mysocket=new Socket ("127.0.0.1", 8002);
            in = new InputStreamReader (Mysocket.getinputstream (), "UTF8"); 
            out = new OutputStreamWriter (Mysocket.getoutputstream ());
            Enter data from the console String sendmsg = "";
            BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
            System.out.println ("Enter Your Value:");


            sendmsg = Br.readline ();        Out.write (sendmsg);
              Write information to the server through out.
            Out.flush ();             
                 
             int c = 0; while ((c = in.read ()) >-1) {//System.out.print ((char)c); Information is written to the output stream by using in-reading the server.                
              Blocking state, S + = (char) c;                 
             //unless read to the information.
             SYSTEM.OUT.PRINTLN ("Customer receives server data---->" +s);
             In.close ();
             Out.close ();  
           Mysocket.close ();
           catch (IOException e) {System.out.println (e.tostring ());
 }

    }
}


C # Server:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net.Sockets;
Using System.Net;

Using System.Threading; Namespace TCPServer {class Program {static private System.Net.IPAddress MyIP = System.Net.IPAddress.Pars
        E ("127.0.0.1");
        static private TcpListener listener = new TcpListener (MyIP, 8002);
        static Private String msg;
            static void Main (string[] args) {threadpool.setmaxthreads (10, 5);
            Threadpool.setminthreads (5, 5); Listener.
            Start ();
                    Receive data while (TRUE) {//test for data try { TcpClient client = listener.
                    AcceptTcpClient ();
                    
                ThreadPool.QueueUserWorkItem (CALLBACKPOC, client); The catch (Exception re) {Console.WriteLine (re).
                message);
     }  } listener.
        Stop ();
            }//thread pool callback function static void Callbackpoc (Object k) {tcpclient client = (tcpclient) k; NetworkStream ns = client.
            GetStream ();
                StreamReader sr = new StreamReader (ns);//stream writer//Word group processing while (true) {
                    try {byte[] bytes = new byte[1024]; int bytesread = ns. Read (bytes, 0, bytes.
                    Length);
                    msg = Encoding.Default.GetString (bytes, 0, bytesread);
                    Display//messagebox.show (msg);
                    Console.WriteLine (msg);
                    Pass the received data back to the client, in practice, should be resolved, the corresponding data back to the client. NetworkStream sendstream = client.
                    GetStream ();
                    String MSGA = Console.ReadLine ();
                    byte[] sendbytes = Encoding.Default.GetBytes (MSGA);
 Sendstream.write (sendbytes, 0, sendbytes.length);                   Sendstream.flush (); Ns.
                    Flush (); Ns.
                    Close ();
                Sendstream.close (); The catch (Exception ex) {client.
                    Close (); Console.WriteLine (ex.
                    message);
                Break
 }
            }
        }
    
    }
}


In addition, there are four C # aspects TCPServer Example program available for study, download address

http://download.csdn.net/source/3232338

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.