简体   繁体   English

如何使用Java中的套接字编程连接到服务器?

[英]How to connect to server using socket programming in Java?

I'm trying to create connection with server with username, password, IP address, port number using socket programming. 我正在尝试使用套接字编程与用户名,密码,IP地址,端口号与服务器建立连接。 I was able to make connection using IP address and port number. 我能够使用IP地址和端口号进行连接。 Program: 程序:

package helloworld;

import java.io.*;  
import java.net.*;
import java.io.IOException;

public class HelloWorld {

    public static void main(String[] args) throws IOException 
    {
        Socket s = new Socket();
        String host = "111.11.11.1";
        int port = 1101;
        String userName = "ssss";
        String Password = "123";
        String message;

        try
        {
            s.connect(new InetSocketAddress(host , port));
        } 
        //Host not found
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        }       
        System.out.println("Connected###");
    } 
}    

But I didn't find the way to include username, and password while making connection. 但是我没有找到建立连接时包括用户名和密码的方法。 Can anyone please help me regarding this? 有人可以帮我吗?

Thanks in advance. 提前致谢。

Just solved the program. 刚解决了程序。 Here is the link: 链接在这里:

Connecting to server using socket programming in Java 使用Java中的套接字编程连接到服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM